Refactoring of OpenRA.Utility. Whole lot of work on OpenRA.Launcher
Mod configuration dialog now fully functional, launch button also works.
This commit is contained in:
committed by
Paul Chote
parent
f98f3d0b39
commit
da384af339
@@ -11,12 +11,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Principal;
|
||||
using System.Threading;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using OpenRA.FileFormats;
|
||||
|
||||
namespace OpenRA.Utility
|
||||
{
|
||||
@@ -36,22 +31,22 @@ namespace OpenRA.Utility
|
||||
static void Main(string[] args)
|
||||
{
|
||||
argCallbacks = new Dictionary<string, ArgCallback>();
|
||||
argCallbacks.Add("--list-mods", ListMods);
|
||||
argCallbacks.Add("-l", ListMods);
|
||||
argCallbacks.Add("--mod-info", ListModInfo);
|
||||
argCallbacks.Add("-i", ListModInfo);
|
||||
argCallbacks.Add("--install-ra-music", InstallRAMusic);
|
||||
argCallbacks.Add("--install-cnc-music", InstallCncMusic);
|
||||
argCallbacks.Add("--download-packages", DownloadPackages);
|
||||
argCallbacks.Add("--install-ra-packages", InstallRAPackages);
|
||||
argCallbacks.Add("--install-cnc-packages", InstallCncPackages);
|
||||
argCallbacks.Add("--settings-value", Settings);
|
||||
argCallbacks.Add("--install-mod", InstallMod);
|
||||
argCallbacks.Add("--list-mods", Command.ListMods);
|
||||
argCallbacks.Add("-l", Command.ListMods);
|
||||
argCallbacks.Add("--mod-info", Command.ListModInfo);
|
||||
argCallbacks.Add("-i", Command.ListModInfo);
|
||||
argCallbacks.Add("--install-ra-music", Command.InstallRAMusic);
|
||||
argCallbacks.Add("--install-cnc-music", Command.InstallCncMusic);
|
||||
argCallbacks.Add("--download-packages", Command.DownloadPackages);
|
||||
argCallbacks.Add("--install-ra-packages", Command.InstallRAPackages);
|
||||
argCallbacks.Add("--install-cnc-packages", Command.InstallCncPackages);
|
||||
argCallbacks.Add("--settings-value", Command.Settings);
|
||||
argCallbacks.Add("--install-mod", Command.InstallMod);
|
||||
|
||||
WindowsIdentity id = WindowsIdentity.GetCurrent();
|
||||
WindowsPrincipal p = new WindowsPrincipal(id);
|
||||
if (p.IsInRole(WindowsBuiltInRole.Administrator))
|
||||
Console.SetOut(new StreamWriter(File.OpenWrite("output.txt")));
|
||||
Console.SetOut(new StreamWriter(File.Create("output.txt")));
|
||||
|
||||
if (args.Length == 0) { PrintUsage(); return; }
|
||||
var arg = SplitArgs(args[0]);
|
||||
@@ -79,215 +74,5 @@ namespace OpenRA.Utility
|
||||
Console.WriteLine(" --settings-value=SUPPORTDIR,KEY Get value of KEY in SUPPORTDIR/settings.yaml");
|
||||
Console.WriteLine(" --install-mod=ZIPFILE Install a mod from ZIPFILE");
|
||||
}
|
||||
|
||||
static void ListMods(string _)
|
||||
{
|
||||
foreach (var m in Mod.AllMods)
|
||||
Console.WriteLine(m.Key);
|
||||
}
|
||||
|
||||
static void ListModInfo(string modList)
|
||||
{
|
||||
string[] mods = modList.Split(',');
|
||||
foreach (var m in mods)
|
||||
{
|
||||
var mod = Mod.AllMods
|
||||
.Where(x => x.Key.Equals(m))
|
||||
.Select(x => x.Value)
|
||||
.FirstOrDefault();
|
||||
if (mod == null)
|
||||
{
|
||||
Console.WriteLine("Error: Mod `{0}` is not installed or could not be found.", m);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("{0}:", m);
|
||||
Console.WriteLine(" Title: {0}", mod.Title);
|
||||
Console.WriteLine(" Version: {0}", mod.Version);
|
||||
Console.WriteLine(" Author: {0}", mod.Author);
|
||||
Console.WriteLine(" Description: {0}", mod.Description);
|
||||
Console.WriteLine(" Requires: {0}", mod.RequiresMods == null ? "" : string.Join(",", mod.RequiresMods));
|
||||
Console.WriteLine(" Standalone: {0}", mod.Standalone.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
static void InstallRAMusic(string path)
|
||||
{
|
||||
ExtractPackagesFromMix(path, string.Format("mods{0}ra{0}packages", Path.DirectorySeparatorChar),
|
||||
"MAIN.MIX", "scores.mix");
|
||||
Console.WriteLine("Done");
|
||||
}
|
||||
|
||||
static void InstallCncMusic(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) { Console.WriteLine("Error: Path {0} does not exist", path); return; }
|
||||
string scoresMixPath = path + Path.DirectorySeparatorChar + "SCORES.MIX";
|
||||
if (!File.Exists(scoresMixPath)) { Console.WriteLine("Error: Could not find SCORES.MIX in path {0}", path); return; }
|
||||
|
||||
File.Copy(scoresMixPath, string.Format("mods{0}cnc{0}packages{0}scores.mix", Path.DirectorySeparatorChar), true);
|
||||
|
||||
Console.WriteLine("Done");
|
||||
}
|
||||
|
||||
static void DownloadPackages(string argValue)
|
||||
{
|
||||
string[] args = argValue.Split(',');
|
||||
string mod = "";
|
||||
string destPath = Path.GetTempPath();
|
||||
|
||||
if (args.Length >= 1)
|
||||
mod = args[0];
|
||||
if (args.Length >= 2)
|
||||
destPath = args[1];
|
||||
|
||||
string destFile = string.Format("{0}{1}{2}-packages.zip", destPath, Path.DirectorySeparatorChar, mod);
|
||||
|
||||
if (File.Exists(destFile))
|
||||
{
|
||||
Console.WriteLine ("Downloaded file already exists, using it instead.");
|
||||
ExtractPackagesFromZip(mod, destPath);
|
||||
return;
|
||||
}
|
||||
|
||||
WebClient wc = new WebClient();
|
||||
wc.DownloadProgressChanged += DownloadProgressChanged;
|
||||
wc.DownloadFileCompleted += DownloadFileCompleted;
|
||||
Console.WriteLine("Downloading {0}-packages.zip to {1}", mod, destPath);
|
||||
wc.DownloadFileAsync(
|
||||
new Uri(string.Format("http://open-ra.org/get-dependency.php?file={0}-packages", mod)),
|
||||
destFile,
|
||||
new string[] { mod, destPath });
|
||||
|
||||
while (wc.IsBusy)
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
static void ExtractPackagesFromMix(string srcPath, string destPath, string mix, params string[] packages)
|
||||
{
|
||||
if (!Directory.Exists(srcPath)) { Console.WriteLine("Error: Path {0} does not exist", srcPath); return; }
|
||||
FileSystem.Mount(srcPath);
|
||||
if (!FileSystem.Exists(mix)) { Console.WriteLine("Error: Could not find {1} in path {0}", srcPath, mix); return; }
|
||||
FileSystem.Mount(mix);
|
||||
|
||||
if (!Directory.Exists(destPath))
|
||||
Directory.CreateDirectory(destPath);
|
||||
|
||||
foreach(string s in packages)
|
||||
{
|
||||
var destFile = "{0}{1}{2}".F(destPath, Path.DirectorySeparatorChar, s);
|
||||
using (var sourceStream = FileSystem.Open(s))
|
||||
using (var destStream = File.Create(destFile))
|
||||
{
|
||||
Console.WriteLine("Extracting {0}", s);
|
||||
destStream.Write(sourceStream.ReadAllBytes());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void ExtractPackagesFromZip(string mod, string dest)
|
||||
{
|
||||
string filepath = string.Format("{0}{1}{2}-packages.zip", dest, Path.DirectorySeparatorChar, mod);
|
||||
string modPackageDir = string.Format("mods{0}{1}{0}packages{0}", Path.DirectorySeparatorChar, mod);
|
||||
|
||||
if (!Directory.Exists(modPackageDir))
|
||||
Directory.CreateDirectory(modPackageDir);
|
||||
|
||||
using (var z = new ZipInputStream(File.OpenRead(filepath)))
|
||||
{
|
||||
ZipEntry entry;
|
||||
while ((entry = z.GetNextEntry()) != null)
|
||||
{
|
||||
if (!entry.IsFile) continue;
|
||||
|
||||
Console.WriteLine ("Extracting {0}", entry.Name);
|
||||
using (var f = File.Create(modPackageDir + entry.Name))
|
||||
{
|
||||
int bufSize = 2048;
|
||||
byte[] buf = new byte[bufSize];
|
||||
while ((bufSize = z.Read(buf, 0, buf.Length)) > 0)
|
||||
{
|
||||
f.Write(buf, 0, bufSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine ("Done");
|
||||
}
|
||||
|
||||
static void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
|
||||
{
|
||||
if (e.Error != null)
|
||||
{
|
||||
Console.WriteLine("Error: {0}", e.Error.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Download Completed");
|
||||
string[] modAndDest = (string[])e.UserState;
|
||||
ExtractPackagesFromZip(modAndDest[0], modAndDest[1]);
|
||||
}
|
||||
|
||||
static void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
|
||||
{
|
||||
Console.WriteLine("{0}% {1}/{2} bytes", e.ProgressPercentage, e.BytesReceived, e.TotalBytesToReceive);
|
||||
}
|
||||
|
||||
static void InstallRAPackages(string path)
|
||||
{
|
||||
ExtractPackagesFromMix(path, "mods{0}ra{0}packages".F(Path.DirectorySeparatorChar), "MAIN.MIX",
|
||||
"conquer.mix", "russian.mix", "allies.mix", "sounds.mix", "scores.mix",
|
||||
"snow.mix", "interior.mix", "temperat.mix");
|
||||
var redalertMixPath = "{0}{1}INSTALL{1}REDALERT.MIX".F(path, Path.DirectorySeparatorChar);
|
||||
if (!File.Exists(redalertMixPath)) { Console.WriteLine ("Error: REDALERT.MIX could not be found on the CD"); return; }
|
||||
Console.WriteLine ("Copying REDALERT.MIX");
|
||||
File.Copy(redalertMixPath, "mods{0}ra{0}packages{0}redalert.mix".F(Path.DirectorySeparatorChar));
|
||||
Console.WriteLine ("Done");
|
||||
}
|
||||
|
||||
static void InstallCncPackages(string path)
|
||||
{
|
||||
Console.WriteLine ("Error: NotI");
|
||||
}
|
||||
|
||||
static void Settings(string argValue)
|
||||
{
|
||||
string[] args = argValue.Split(',');
|
||||
|
||||
if (args.Length < 2) { return; }
|
||||
|
||||
string settingsFile = args[0] + Path.DirectorySeparatorChar + "settings.yaml";
|
||||
List<MiniYamlNode> settingsYaml = MiniYaml.FromFile(settingsFile);
|
||||
Queue<String> settingKey = new Queue<string>(args[1].Split('.'));
|
||||
|
||||
string s = settingKey.Dequeue();
|
||||
MiniYaml n = settingsYaml.Where(x => x.Key == s).Select(x => x.Value).FirstOrDefault();
|
||||
|
||||
if (n == null)
|
||||
{
|
||||
Console.WriteLine("Error: Could not find {0} in {1}", args[1], settingsFile);
|
||||
return;
|
||||
}
|
||||
|
||||
while (settingKey.Count > 0)
|
||||
{
|
||||
s = settingKey.Dequeue();
|
||||
if (!n.NodesDict.TryGetValue(s, out n))
|
||||
{
|
||||
Console.WriteLine("Error: Could not find {0} in {1}", args[1], settingsFile);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(n.Value);
|
||||
}
|
||||
|
||||
static void InstallMod(string zipFile)
|
||||
{
|
||||
if (!File.Exists(zipFile)) { Console.WriteLine("Error: Could not find {0}", zipFile); return; }
|
||||
using (var zipStream = new ZipInputStream(File.OpenRead(zipFile)))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user