Commit 8fb06838 authored by Szefo09's avatar Szefo09 Committed by Unicorn

AutoUpdate written?

parent 58ec23e4
......@@ -3,6 +3,8 @@ using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;
using System.Net;
public class Program : MonoBehaviour
{
......@@ -280,8 +282,6 @@ public class Program : MonoBehaviour
//保持唤醒
Screen.sleepTimeout = SleepTimeout.NeverSleep;
//创建资源目录
if (!Directory.Exists("/storage/emulated/0/ygocore"))
{
DirPaths("/storage/emulated/0/ygocore/cdb/");
DirPaths("/storage/emulated/0/ygocore/config/");
DirPaths("/storage/emulated/0/ygocore/deck/");
......@@ -296,8 +296,18 @@ public class Program : MonoBehaviour
DirPaths("/storage/emulated/0/ygocore/textures/duel/healthBar/");
DirPaths("/storage/emulated/0/ygocore/textures/duel/phase/");
DirPaths("/storage/emulated/0/ygocore/textures/ui/");
DirPaths("/storage/emulated/0/ygocore/updates/");
if(!File.Exists("/storage/emulated/0/ygocore/.nomedia"))
{
File.Create("/storage/emulated/0/ygocore/.nomedia");
File.Create("/storage/emulated/0/ygocore/expansions/pics/.nomedia");
}
if(!File.Exists("/storage/emulated/0/ygocore/expansions/.nomedia"))
{
File.Create("/storage/emulated/0/ygocore/expansions/.nomedia");
}
if(!File.Exists("/storage/emulated/0/ygocore/picture/.nomedia"))
{
File.Create("/storage/emulated/0/ygocore/picture/.nomedia");
}
Environment.CurrentDirectory = "/storage/emulated/0/ygocore";
System.IO.Directory.SetCurrentDirectory("/storage/emulated/0/ygocore");
......@@ -316,23 +326,26 @@ public class Program : MonoBehaviour
});
go(300, () =>
{
UpdateClient();
InterString.initialize("config/translation.conf");
//InterString.initialize("config" + AppLanguage.LanguageDir() + "/translation.conf"); //System Language
GameTextureManager.initialize();
Config.initialize("config/config.conf");
//GameStringManager.initialize("config/strings.conf");
GameStringManager.initialize("strings.conf");
if (File.Exists("cdb/strings.conf"))
if (File.Exists("config/strings.conf"))
{
GameStringManager.initialize("cdb/strings.conf");
GameStringManager.initialize("config/strings.conf");
}
if (File.Exists("expansions/strings.conf"))
{
GameStringManager.initialize("expansions/strings.conf");
}
//YGOSharp.BanlistManager.initialize("config/lflist.conf");
YGOSharp.BanlistManager.initialize("lflist.conf");
var fileInfos = (new DirectoryInfo("cdb")).GetFiles();
//var fileInfos = (new DirectoryInfo("cdb" + AppLanguage.LanguageDir())).GetFiles();//System Language
FileInfo[] fileInfos = (new DirectoryInfo("cdb")).GetFiles().OrderByDescending(x => x.Name).ToArray();
//FileInfo[] fileInfos = (new DirectoryInfo("cdb" + AppLanguage.LanguageDir())).GetFiles().OrderByDescending(x => x.Name).ToArray();//System Language
for (int i = 0; i < fileInfos.Length; i++)
{
if (fileInfos[i].Name.Length > 4)
......@@ -348,8 +361,8 @@ public class Program : MonoBehaviour
if (Directory.Exists("expansions"))
//if (Directory.Exists("expansions" + AppLanguage.LanguageDir()))
{
fileInfos = (new DirectoryInfo("expansions")).GetFiles();
//fileInfos = (new DirectoryInfo("expansions" + AppLanguage.LanguageDir())).GetFiles();
fileInfos = (new DirectoryInfo("expansions")).GetFiles().OrderByDescending(x => x.Name).ToArray();
//fileInfos = (new DirectoryInfo("expansions" + AppLanguage.LanguageDir())).GetFiles().OrderByDescending(x => x.Name).ToArray();
for (int i = 0; i < fileInfos.Length; i++)
{
if (fileInfos[i].Name.Length > 4)
......@@ -385,6 +398,52 @@ public class Program : MonoBehaviour
}
private void UpdateClient()
{
try
{
WWW w = new WWW("https://api.github.com/repos/szefo09/updateYGOPro2/contents/");
while (!w.isDone)
{
}
List<ApiFile> toDownload = new List<ApiFile>();
List<ApiFile> apiFromGit = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<ApiFile>>(w.text);
if (!File.Exists("updates/SHAs.txt"))
{
toDownload.AddRange(apiFromGit);
}
if (File.Exists("updates/SHAs.txt"))
{
List<ApiFile> local = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<ApiFile>>(File.ReadAllText("updates/SHAs.txt"));
foreach(ApiFile file in apiFromGit)
{
if(file.sha != local.First(x => x.name == file.name).sha)
{
toDownload.Add(file);
}
}
}
HttpDldFile httpDldFile = new HttpDldFile();
foreach (var dl in toDownload)
{
if (Path.GetExtension(dl.name)==".cdb")
{
httpDldFile.Download(dl.download_url, Path.Combine("cdb", dl.name));
}
if (Path.GetExtension(dl.name) == ".conf")
{
httpDldFile.Download(dl.download_url, Path.Combine("config", dl.name));
}
}
File.WriteAllText("updates/SHAs.txt", w.text);
}
catch (Exception e)
{
Debug.Log(e);
}
}
public GameObject mouseParticle;
static int lastChargeTime = 0;
......
public class Links
{
public string self { get; set; }
public string git { get; set; }
public string html { get; set; }
}
public class ApiFile
{
public string name { get; set; }
public string path { get; set; }
public string sha { get; set; }
public int size { get; set; }
public string url { get; set; }
public string html_url { get; set; }
public string git_url { get; set; }
public string download_url { get; set; }
public string type { get; set; }
public Links _links { get; set; }
}
\ No newline at end of file
fileFormatVersion: 2
guid: 40ff7876fde6c9c4982e888bf428b76e
timeCreated: 1547757047
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -4,47 +4,30 @@ using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
public class HttpDldFile
{
public bool Download(string url, string filename)
{
bool flag = false;
long startPosition = 0;
FileStream writeStream;
if (File.Exists(filename + ".tmp"))
try
{
writeStream = File.OpenWrite(filename + ".tmp");
startPosition = writeStream.Length;
writeStream.Seek(startPosition, SeekOrigin.Current);
if(!Directory.Exists(Path.GetDirectoryName(filename))){
Directory.CreateDirectory(Path.GetDirectoryName(filename));
}
else
using (var client = new WebClient())
{
writeStream = new FileStream(filename + ".tmp", FileMode.Create);
startPosition = 0;
ServicePointManager.ServerCertificateValidationCallback = MyRemoteCertificateValidationCallback;
//client.Headers.Add(HttpRequestHeader.Authorization, string.Concat("token ", RepoData.GetToken()));
client.Headers.Add(HttpRequestHeader.ContentType,"application/x-www-form-urlencoded; charset=UTF-8");
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063");
client.DownloadFile(new Uri(url), filename+".tmp");
}
try
{
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
if (startPosition > 0)
{
myRequest.AddRange((int)startPosition);
}
Stream readStream = myRequest.GetResponse().GetResponseStream();
byte[] btArray = new byte[2048];
int contentSize = readStream.Read(btArray, 0, btArray.Length);
while (contentSize > 0)
{
writeStream.Write(btArray, 0, contentSize);
contentSize = readStream.Read(btArray, 0, btArray.Length);
}
writeStream.Close();
readStream.Close();
flag = true;
if(File.Exists(filename))
flag = true;
if(File.Exists(filename))
{
File.Delete(filename);
}
......@@ -52,10 +35,37 @@ public class HttpDldFile
}
catch (Exception)
{
writeStream.Close();
flag = false;
}
return flag;
}
public static bool MyRemoteCertificateValidationCallback(System.Object sender,
X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
bool isOk = true;
// If there are errors in the certificate chain,
// look at each error to determine the cause.
if (sslPolicyErrors != SslPolicyErrors.None)
{
for (int i = 0; i < chain.ChainStatus.Length; i++)
{
if (chain.ChainStatus[i].Status == X509ChainStatusFlags.RevocationStatusUnknown)
{
continue;
}
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
bool chainIsValid = chain.Build((X509Certificate2)certificate);
if (!chainIsValid)
{
isOk = false;
break;
}
}
}
return isOk;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment