-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateHelper.cs
More file actions
134 lines (118 loc) · 3.32 KB
/
UpdateHelper.cs
File metadata and controls
134 lines (118 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;
using UnityEngine;
namespace NeoModLoader.AutoUpdate;
public enum UpdateResult
{
IsTaken,
Success,
NoNeedUpdate,
Fail
}
public static class UpdateHelper
{
private static readonly Dictionary<string, byte[]> file_bak = new();
public static bool BackupFile(string file_path)
{
if (!File.Exists(file_path)) return true;
file_bak[file_path] = File.ReadAllBytes(file_path);
try
{
File.Delete(file_path);
return true;
}
catch (Exception)
{
// ignored
}
return false;
}
public static void RestoreFile(string file_path)
{
File.WriteAllBytes(file_path, file_bak[file_path]);
file_bak.Remove(file_path);
}
public static void LoadNMLManually()
{
var nml_bytes = File.ReadAllBytes(Paths.NMLPath);
Assembly assembly;
if (File.Exists(Paths.NMLPdbPath))
assembly = Assembly.Load(nml_bytes, File.ReadAllBytes(Paths.NMLPdbPath));
else
assembly = Assembly.Load(nml_bytes);
Type type = assembly.GetType("NeoModLoader.WorldBoxMod");
new GameObject("NeoModLoader")
{
transform =
{
parent = WorldBoxMod.I.transform.parent
}
}.AddComponent(type);
ModLoader.modsLoaded.Add("NeoModLoader");
Debug.Log("[NeoModLoader] Was added manually");
}
public static UpdateResult TryReplaceFile(string pOldFile, string pNewFile)
{
FileInfo old_file = new(pOldFile);
try
{
if (old_file.Exists)
old_file.Delete();
}
catch (Exception e)
{
return UpdateResult.IsTaken;
}
File.Copy(pNewFile, pOldFile, true);
return UpdateResult.Success;
}
public static bool CheckValid(string file_path)
{
try
{
AssemblyName.GetAssemblyName(file_path);
return true;
}
catch (Exception)
{
return false;
}
}
public static Version GetDLLVersion(string file_path)
{
try
{
var assembly_name = AssemblyName.GetAssemblyName(file_path);
return assembly_name.Version;
}
catch (Exception)
{
return new Version(0, 0, 0, 0);
}
}
public static async Task<string> DownloadFile(string download_url, string postfix, string filename)
{
var components = filename.Split('.');
var download_path = Path.Combine(Path.GetTempPath(), $"{components[0]}_{postfix}.{components[1]}");
var download_complete_path =
Path.Combine(Path.GetTempPath(), $"{components[0]}_{postfix}_completed.{components[1]}");
if (!File.Exists(download_complete_path))
{
using var client = new WebClient();
try
{
await HttpUtils.DownloadFile(download_url, download_path);
}
catch (Exception)
{
return "";
}
File.Move(download_path, download_complete_path);
}
return download_complete_path;
}
}