mirror of
https://e.coding.net/circlecloud/CTZLauncher.git
synced 2025-11-24 21:36:06 +00:00
合并库文件到EXE...
This commit is contained in:
76
CTZLauncher/Tools/SystemTools.cs
Normal file
76
CTZLauncher/Tools/SystemTools.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
namespace KMCCC.Tools
|
||||
{
|
||||
#region
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.VisualBasic.Devices;
|
||||
using Microsoft.Win32;
|
||||
|
||||
#endregion
|
||||
|
||||
public class SystemTools
|
||||
{
|
||||
/// <summary>
|
||||
/// 从注册表中查找可能的javaw.exe位置
|
||||
/// </summary>
|
||||
/// <returns>JAVA地址列表</returns>
|
||||
public static IEnumerable<string> FindJava()
|
||||
{
|
||||
try
|
||||
{
|
||||
var rootReg = Registry.LocalMachine.OpenSubKey("SOFTWARE");
|
||||
return rootReg == null
|
||||
? new string[0]
|
||||
: FindJavaInternal(rootReg).Union(FindJavaInternal(rootReg.OpenSubKey("Wow6432Node")));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new string[0];
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> FindJavaInternal(RegistryKey registry)
|
||||
{
|
||||
try
|
||||
{
|
||||
var registryKey = registry.OpenSubKey("JavaSoft");
|
||||
if ((registryKey == null) || ((registry = registryKey.OpenSubKey("Java Runtime Environment")) == null)) return new string[0];
|
||||
return (from ver in registry.GetSubKeyNames()
|
||||
select registry.OpenSubKey(ver)
|
||||
into command
|
||||
where command != null
|
||||
select command.GetValue("JavaHome")
|
||||
into javaHomes
|
||||
where javaHomes != null
|
||||
select javaHomes.ToString()
|
||||
into str
|
||||
where !String.IsNullOrWhiteSpace(str)
|
||||
select str + @"\bin\javaw.exe");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new string[0];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取物理内存
|
||||
/// </summary>
|
||||
/// <returns>物理内存</returns>
|
||||
public static ulong GetTotalMemory()
|
||||
{
|
||||
return new Computer().Info.TotalPhysicalMemory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取x86 or x64
|
||||
/// </summary>
|
||||
/// <returns>32 or 64</returns>
|
||||
public static string GetArch()
|
||||
{
|
||||
return Environment.Is64BitOperatingSystem ? "64" : "32";
|
||||
}
|
||||
}
|
||||
}
|
||||
54
CTZLauncher/Tools/UsefulTools.cs
Normal file
54
CTZLauncher/Tools/UsefulTools.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
namespace KMCCC.Tools
|
||||
{
|
||||
#region
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 有用的东西
|
||||
/// </summary>
|
||||
public static class UsefulTools
|
||||
{
|
||||
public static string DoReplace(this string source, IDictionary<string, string> dic)
|
||||
{
|
||||
return dic.Aggregate(source, (current, pair) => current.Replace("${" + pair.Key + "}", pair.Value));
|
||||
}
|
||||
|
||||
public static string GoString(this Guid? guid)
|
||||
{
|
||||
if (guid == null)
|
||||
return "{}";
|
||||
return guid.ToString().Replace("-", "");
|
||||
}
|
||||
|
||||
public static void Dircopy(string source, string target)
|
||||
{
|
||||
var sourceDir = new DirectoryInfo(source);
|
||||
if (!Directory.Exists(target))
|
||||
{
|
||||
Directory.CreateDirectory(target);
|
||||
}
|
||||
foreach (var file in sourceDir.GetFiles())
|
||||
{
|
||||
File.Copy(file.FullName, target + "\\" + file.Name, true);
|
||||
}
|
||||
foreach (var subdir in sourceDir.GetDirectories())
|
||||
{
|
||||
Dircopy(subdir.FullName, target + "\\" + subdir.Name);
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
public static string Print(this string str)
|
||||
{
|
||||
Console.WriteLine(str);
|
||||
return str;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
227
CTZLauncher/Tools/ZipTools.cs
Normal file
227
CTZLauncher/Tools/ZipTools.cs
Normal file
@@ -0,0 +1,227 @@
|
||||
namespace KMCCC.Tools
|
||||
{
|
||||
#region
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO.Packaging;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 操蛋的通过反射调用Zip解压
|
||||
/// </summary>
|
||||
public static class ZipTools
|
||||
{
|
||||
public static readonly Boolean Enabled;
|
||||
|
||||
public static readonly Type ZipArchive;
|
||||
|
||||
public static readonly MethodInfo ZipArchive_OpenOnFile;
|
||||
|
||||
public static readonly MethodInfo ZipArchive_GetFiles;
|
||||
|
||||
public static readonly FieldInfo ZipArchive_ZipIOBlockManager;
|
||||
|
||||
public static readonly Type ZipFileInfo;
|
||||
|
||||
public static readonly MethodInfo ZipFileInfo_GetStream;
|
||||
|
||||
public static readonly PropertyInfo ZipFileInfo_Name;
|
||||
|
||||
public static readonly PropertyInfo ZipFileInfo_FolderFlag;
|
||||
|
||||
public static readonly Type ZipIOBlockManager;
|
||||
|
||||
public static readonly FieldInfo ZipIOBlockManager_Encoding;
|
||||
|
||||
static ZipTools()
|
||||
{
|
||||
try
|
||||
{
|
||||
var windowsBase = typeof (Package).Assembly;
|
||||
ZipArchive = windowsBase.GetType("MS.Internal.IO.Zip.ZipArchive");
|
||||
ZipArchive_OpenOnFile = ZipArchive.GetMethod("OpenOnFile", BindingFlags.NonPublic | BindingFlags.Static);
|
||||
ZipArchive_GetFiles = ZipArchive.GetMethod("GetFiles", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
ZipArchive_ZipIOBlockManager = ZipArchive.GetField("_blockManager", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
ZipFileInfo = windowsBase.GetType("MS.Internal.IO.Zip.ZipFileInfo");
|
||||
ZipFileInfo_GetStream = ZipFileInfo.GetMethod("GetStream", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
ZipFileInfo_Name = ZipFileInfo.GetProperty("Name", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
ZipFileInfo_FolderFlag = ZipFileInfo.GetProperty("FolderFlag", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
ZipIOBlockManager = windowsBase.GetType("MS.Internal.IO.Zip.ZipIOBlockManager");
|
||||
ZipIOBlockManager_Encoding = ZipIOBlockManager.GetField("_encoding", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
|
||||
Enabled = true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
Enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Unzip(string zipFile, string outputDirectory, UnzipOptions options)
|
||||
{
|
||||
return UnzipFile(zipFile, outputDirectory, options) == null;
|
||||
}
|
||||
|
||||
public static Exception UnzipFile(string zipFile, string outputDirectory, UnzipOptions options)
|
||||
{
|
||||
options = options ?? new UnzipOptions();
|
||||
try
|
||||
{
|
||||
var root = new DirectoryInfo(outputDirectory);
|
||||
root.Create();
|
||||
var rootPath = root.FullName + "/";
|
||||
using (var zip = (IDisposable) ZipArchive_OpenOnFile.Invoke(null, new object[] {zipFile, FileMode.Open, FileAccess.Read, FileShare.Read, false}))
|
||||
{
|
||||
var ioManager = ZipArchive_ZipIOBlockManager.GetValue(zip);
|
||||
ZipIOBlockManager_Encoding.SetValue(ioManager, new WarpedEncoding(options.Encoding ?? Encoding.Default));
|
||||
|
||||
var files = (IEnumerable) ZipArchive_GetFiles.Invoke(zip, new object[] {});
|
||||
IEnumerable<string> exclude = (options.Exclude ?? new List<string>());
|
||||
if (exclude.Count() > 1000)
|
||||
{
|
||||
exclude = exclude.AsParallel();
|
||||
}
|
||||
|
||||
foreach (var item in files)
|
||||
{
|
||||
var name = (string) ZipFileInfo_Name.GetValue(item, null);
|
||||
if (exclude.Any(name.StartsWith))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if ((bool) ZipFileInfo_FolderFlag.GetValue(item, null))
|
||||
{
|
||||
Directory.CreateDirectory(rootPath + name);
|
||||
continue;
|
||||
}
|
||||
using (var stream = (Stream) ZipFileInfo_GetStream.Invoke(item, new object[] {FileMode.Open, FileAccess.Read}))
|
||||
{
|
||||
var filePath = rootPath + name;
|
||||
var directoryInfo = new FileInfo(filePath).Directory;
|
||||
if (directoryInfo != null) directoryInfo.Create();
|
||||
using (var fs = new FileStream(filePath, FileMode.Create))
|
||||
{
|
||||
stream.CopyTo(fs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return exp;
|
||||
}
|
||||
}
|
||||
|
||||
#region 编码大法
|
||||
|
||||
public class WarpedEncoding : ASCIIEncoding
|
||||
{
|
||||
private readonly Encoding _innerEncoding;
|
||||
|
||||
public WarpedEncoding(Encoding encoding)
|
||||
{
|
||||
_innerEncoding = encoding ?? Default;
|
||||
}
|
||||
|
||||
public override bool Equals(object value)
|
||||
{
|
||||
return _innerEncoding.Equals(value);
|
||||
}
|
||||
|
||||
public override int GetByteCount(char[] chars, int index, int count)
|
||||
{
|
||||
return _innerEncoding.GetByteCount(chars, index, count);
|
||||
}
|
||||
|
||||
public override int GetByteCount(string chars)
|
||||
{
|
||||
return _innerEncoding.GetByteCount(chars);
|
||||
}
|
||||
|
||||
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
return _innerEncoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex);
|
||||
}
|
||||
|
||||
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
|
||||
{
|
||||
return _innerEncoding.GetBytes(s, charIndex, charCount, bytes, byteIndex);
|
||||
}
|
||||
|
||||
public override int GetCharCount(byte[] bytes, int index, int count)
|
||||
{
|
||||
return _innerEncoding.GetCharCount(bytes, index, count);
|
||||
}
|
||||
|
||||
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
|
||||
{
|
||||
return _innerEncoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex);
|
||||
}
|
||||
|
||||
public override Decoder GetDecoder()
|
||||
{
|
||||
return _innerEncoding.GetDecoder();
|
||||
}
|
||||
|
||||
public override Encoder GetEncoder()
|
||||
{
|
||||
return _innerEncoding.GetEncoder();
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _innerEncoding.GetHashCode();
|
||||
}
|
||||
|
||||
public override int GetMaxByteCount(int charCount)
|
||||
{
|
||||
return _innerEncoding.GetMaxByteCount(charCount);
|
||||
}
|
||||
|
||||
public override int GetMaxCharCount(int byteCount)
|
||||
{
|
||||
return _innerEncoding.GetMaxCharCount(byteCount);
|
||||
}
|
||||
|
||||
public override byte[] GetPreamble()
|
||||
{
|
||||
return _innerEncoding.GetPreamble();
|
||||
}
|
||||
|
||||
public override string GetString(byte[] bytes, int index, int count)
|
||||
{
|
||||
return _innerEncoding.GetString(bytes, index, count);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 解压选项
|
||||
/// </summary>
|
||||
public class UnzipOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 排除的文件(夹)
|
||||
/// </summary>
|
||||
public List<string> Exclude { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 文件(夹)名使用的编码
|
||||
/// </summary>
|
||||
public Encoding Encoding { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user