tools/builder/ABuilder.cs
author Matt Woodrow <pred@alliedmods.net>
Tue Apr 01 00:51:21 2008 +0000 (2008-04-01)
changeset 1956 8ae29b9df92c
parent 1952 b7aa2b626807
child 2260 d306de5f9fa5
permissions -rw-r--r--
Added EquipPlayerWeapon to SDKTools. Offsets for CS:S and TF2 added.
ds@1952
     1
using System;
ds@1952
     2
using System.IO;
ds@1952
     3
using System.Diagnostics;
ds@1952
     4
ds@1952
     5
namespace builder
ds@1952
     6
{
ds@1952
     7
	public abstract class ABuilder
ds@1952
     8
	{
ds@1952
     9
		public Config cfg;
ds@1952
    10
ds@1952
    11
		public ABuilder()
ds@1952
    12
		{
ds@1952
    13
		}
ds@1952
    14
ds@1952
    15
		public abstract bool BuildLibrary(Package pkg, Library lib);
ds@1952
    16
ds@1952
    17
		public abstract string GetPawnCompilerName();
ds@1952
    18
ds@1952
    19
		public bool CompilePlugin(Package pkg, Plugin pl)
ds@1952
    20
		{
ds@1952
    21
			string local_dir = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting", cfg.pkg_path, pkg.GetBaseFolder());
ds@1952
    22
ds@1952
    23
			string filepath = null;
ds@1952
    24
			if (pl.Folder != null)
ds@1952
    25
			{
ds@1952
    26
				filepath = Config.PathFormat("{0}/{1}", pl.Folder, pl.Source);
ds@1952
    27
			} 
ds@1952
    28
			else 
ds@1952
    29
			{
ds@1952
    30
				filepath = pl.Source;
ds@1952
    31
			}
ds@1952
    32
ds@1952
    33
			ProcessStartInfo info = new ProcessStartInfo();
ds@1952
    34
			info.WorkingDirectory = local_dir;
ds@1952
    35
			info.FileName = Config.PathFormat("{0}/{1}", local_dir, GetPawnCompilerName());
ds@1952
    36
			info.Arguments = filepath + ".sp";
ds@1952
    37
			info.UseShellExecute = false;
ds@1952
    38
			info.RedirectStandardOutput = true;
ds@1952
    39
			info.RedirectStandardError = true;
ds@1952
    40
ds@1952
    41
			Process p = Process.Start(info);
ds@1952
    42
			string output = p.StandardOutput.ReadToEnd() + "\n";
ds@1952
    43
			output += p.StandardError.ReadToEnd();
ds@1952
    44
			p.WaitForExit();
ds@1952
    45
			p.Close();
ds@1952
    46
ds@1952
    47
			Console.WriteLine("Debug: wd = " + info.WorkingDirectory + " fn = " + info.FileName + " arg = " + info.Arguments);
ds@1952
    48
			Console.WriteLine(output);
ds@1952
    49
ds@1952
    50
			string binary = Config.PathFormat("{0}/{1}/addons/sourcemod/scripting/{2}.smx", cfg.pkg_path, pkg.GetBaseFolder(), pl.Source);
ds@1952
    51
			if (!File.Exists(binary))
ds@1952
    52
			{
ds@1952
    53
				Console.WriteLine("Could not find binary: " + binary);
ds@1952
    54
				return false;
ds@1952
    55
			}
ds@1952
    56
ds@1952
    57
			string new_loc;
ds@1952
    58
			if (pl.disabled)
ds@1952
    59
			{
ds@1952
    60
				new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/disabled/{2}.smx", cfg.pkg_path, pkg.GetBaseFolder(), pl.Source);
ds@1952
    61
			} 
ds@1952
    62
			else 
ds@1952
    63
			{
ds@1952
    64
				new_loc = Config.PathFormat("{0}/{1}/addons/sourcemod/plugins/{2}.smx", cfg.pkg_path, pkg.GetBaseFolder(), pl.Source);
ds@1952
    65
			}
ds@1952
    66
ds@1952
    67
			try
ds@1952
    68
			{
ds@1952
    69
				if (File.Exists(new_loc))
ds@1952
    70
				{
ds@1952
    71
					File.Delete(new_loc);
ds@1952
    72
				}
ds@1952
    73
				File.Move(binary, new_loc);
ds@1952
    74
			}
ds@1952
    75
			catch (System.Exception e)
ds@1952
    76
			{
ds@1952
    77
				Console.WriteLine(e.Message);
ds@1952
    78
				return false;
ds@1952
    79
			}
ds@1952
    80
ds@1952
    81
			return true;
ds@1952
    82
		}
ds@1952
    83
ds@1952
    84
		public bool CopyFile(Package pkg, string source, string dest)
ds@1952
    85
		{
ds@1952
    86
			string from = Config.PathFormat("{0}/{1}", 
ds@1952
    87
				cfg.source_path,
ds@1952
    88
				source);
ds@1952
    89
			string to = Config.PathFormat("{0}/{1}/{2}",
ds@1952
    90
				cfg.pkg_path,
ds@1952
    91
				pkg.GetBaseFolder(),
ds@1952
    92
				dest);
ds@1952
    93
ds@1952
    94
			File.Copy(from, to, true);
ds@1952
    95
ds@1952
    96
			return true;
ds@1952
    97
		}
ds@1952
    98
ds@1952
    99
		/** dest can be null to mean root base folder */
ds@1952
   100
		public void CopyFolder(Package pkg, string source, string dest, string [] omits)
ds@1952
   101
		{
ds@1952
   102
			string from_base = Config.PathFormat("{0}/{1}", cfg.source_path, source);
ds@1952
   103
			string to_base = null;
ds@1952
   104
			
ds@1952
   105
			if (dest == null)
ds@1952
   106
			{
ds@1952
   107
				to_base = Config.PathFormat("{0}/{1}", 
ds@1952
   108
					cfg.pkg_path, 
ds@1952
   109
					pkg.GetBaseFolder());
ds@1952
   110
			} 
ds@1952
   111
			else 
ds@1952
   112
			{
ds@1952
   113
				to_base = Config.PathFormat("{0}/{1}/{2}", 
ds@1952
   114
					cfg.pkg_path, 
ds@1952
   115
					pkg.GetBaseFolder(), 
ds@1952
   116
					dest);
ds@1952
   117
			}
ds@1952
   118
ds@1952
   119
			string [] files = Directory.GetFiles(from_base);
ds@1952
   120
			string file;
ds@1952
   121
ds@1952
   122
			for (int i=0; i<files.Length; i++)
ds@1952
   123
			{
ds@1952
   124
				file = Path.GetFileName(files[i]);
ds@1952
   125
ds@1952
   126
				if (omits != null)
ds@1952
   127
				{
ds@1952
   128
					bool skip = false;
ds@1952
   129
					for (int j=0; j<omits.Length; j++)
ds@1952
   130
					{
ds@1952
   131
						if (file.CompareTo(omits[j]) == 0)
ds@1952
   132
						{
ds@1952
   133
							skip = true;
ds@1952
   134
							break;
ds@1952
   135
						}
ds@1952
   136
					}
ds@1952
   137
					if (skip)
ds@1952
   138
					{
ds@1952
   139
						continue;
ds@1952
   140
					}
ds@1952
   141
				}
ds@1952
   142
				dest = Config.PathFormat("{0}/{1}", to_base, file);
ds@1952
   143
				File.Copy(files[i], dest, true);
ds@1952
   144
			}
ds@1952
   145
		}
ds@1952
   146
ds@1952
   147
		public void BuildPackage(Package pkg)
ds@1952
   148
		{
ds@1952
   149
			string path = Config.PathFormat("{0}/{1}", cfg.pkg_path, pkg.GetBaseFolder());
ds@1952
   150
ds@1952
   151
			if (!Directory.Exists(path))
ds@1952
   152
			{
ds@1952
   153
				Directory.CreateDirectory(path);
ds@1952
   154
			}
ds@1952
   155
ds@1952
   156
			/* Create all dirs */
ds@1952
   157
			string [] paths = pkg.GetFolders();
ds@1952
   158
			for (int i=0; i<paths.GetLength(0); i++)
ds@1952
   159
			{
ds@1952
   160
				path = Config.PathFormat("{0}/{1}/{2}", cfg.pkg_path, pkg.GetBaseFolder(), paths[i]);
ds@1952
   161
				if (!Directory.Exists(path))
ds@1952
   162
				{
ds@1952
   163
					Directory.CreateDirectory(path);
ds@1952
   164
				}
ds@1952
   165
			}
ds@1952
   166
ds@1952
   167
			/* Do primitive copies */
ds@1952
   168
			pkg.OnCopyFolders(this);
ds@1952
   169
			pkg.OnCopyFiles(this);
ds@1952
   170
ds@1952
   171
			/* Do libraries */
ds@1952
   172
			Library [] libs = pkg.GetLibraries();
ds@1952
   173
			for (int i=0; i<libs.Length; i++)
ds@1952
   174
			{
ds@1952
   175
				if (libs[i].build_mode == BuildMode.BuildMode_Episode1)
ds@1952
   176
				{
ds@1952
   177
					continue;
ds@1952
   178
				}
ds@1952
   179
				if (!BuildLibrary(pkg, libs[i]))
ds@1952
   180
				{
ds@1952
   181
					throw new System.Exception("Failed to compile library: " + libs[i].binary_name);
ds@1952
   182
				}
ds@1952
   183
			}
ds@1952
   184
ds@1952
   185
			/* Do plugins */
ds@1952
   186
			Plugin [] plugins = pkg.GetPlugins();
ds@1952
   187
			if (plugins != null)
ds@1952
   188
			{
ds@1952
   189
				for (int i=0; i<plugins.Length; i++)
ds@1952
   190
				{
ds@1952
   191
					if (!CompilePlugin(pkg, plugins[i]))
ds@1952
   192
					{
ds@1952
   193
						throw new System.Exception("Failed to compile plugin: " + plugins[i].Source);
ds@1952
   194
					}
ds@1952
   195
				}
ds@1952
   196
			}
ds@1952
   197
		}
ds@1952
   198
	}
ds@1952
   199
}