Skip to content

Commit

Permalink
Merge pull request #10 from AxeelAnder/master
Browse files Browse the repository at this point in the history
Version 1.1.1.4
  • Loading branch information
AxeelAnder authored Oct 6, 2019
2 parents 9d8d36e + 308a01c commit a229f3f
Show file tree
Hide file tree
Showing 68 changed files with 610 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/Localizer/Plugins
32 changes: 32 additions & 0 deletions Localizer/DataModel/Default/BasicPrefixFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using Localizer.Attributes;

namespace Localizer.DataModel.Default
{
public class PrefixEntry : IEntry
{
[TModLocalizeTextProp("DisplayName")] public BaseEntry Name { get; set; }

public IEntry Clone()
{
return new PrefixEntry {Name = Name.Clone() as BaseEntry};
}
}

public class BasicPrefixFile : IFile
{
[TModLocalizeField("prefixes")]
public Dictionary<string, PrefixEntry> Prefixes { get; set; } = new Dictionary<string, PrefixEntry>();

public List<string> GetKeys()
{
return Prefixes.Keys.ToList();
}

public IEntry GetValue(string key)
{
return Prefixes[key];
}
}
}
32 changes: 32 additions & 0 deletions Localizer/DataModel/Default/BasicProjectileFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Linq;
using Localizer.Attributes;

namespace Localizer.DataModel.Default
{
public class ProjectileEntry : IEntry
{
[TModLocalizeTextProp("DisplayName")] public BaseEntry Name { get; set; }

public IEntry Clone()
{
return new ProjectileEntry {Name = Name.Clone() as BaseEntry};
}
}

public class BasicProjectileFile : IFile
{
[TModLocalizeField("projectiles")]
public Dictionary<string, ProjectileEntry> Projectiles { get; set; } = new Dictionary<string, ProjectileEntry>();

public List<string> GetKeys()
{
return Projectiles.Keys.ToList();
}

public IEntry GetValue(string key)
{
return Projectiles[key];
}
}
}
45 changes: 45 additions & 0 deletions Localizer/DataModel/Default/GitHubUpdateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;

namespace Localizer.DataModel.Default
{
public class GitHubUpdateInfo : IUpdateInfo
{
public UpdateType Type => type;

public Version Version => version;

private UpdateType type;
private Version version;

private string versionString;

public GitHubUpdateInfo(string versionString)
{
this.versionString = versionString;

Parse();
}

internal void Parse()
{
type = ParseType(versionString[0]);

version = Version.Parse(versionString.Substring(1, versionString.Length - 1));
}

internal UpdateType ParseType(char t)
{
switch (t)
{
case 'a':
return UpdateType.Minor;
case 'b':
return UpdateType.Major;
case 'c':
return UpdateType.Critical;
default:
return UpdateType.None;
}
}
}
}
19 changes: 19 additions & 0 deletions Localizer/DataModel/IUpdateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace Localizer.DataModel
{
public enum UpdateType
{
None,
Minor,
Major,
Critical,
}

public interface IUpdateInfo
{
UpdateType Type { get; }

Version Version { get; }
}
}
File renamed without changes.
22 changes: 22 additions & 0 deletions Localizer/Helpers/UI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Reflection;
using Terraria.ModLoader;
using Terraria.UI;

namespace Localizer
{
public static class UI
{
public static void ShowInfoMessage(string message, int gotoMenu, UIState state = null, string altButtonText = "", Action altButtonAction = null)
{

}

public static object GetModLoaderUI(string uiName)
{
var ui = typeof(Mod).Module.GetType("Terraria.ModLoader.Interface");

return ui.GetField(uiName, BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
}
}
}
83 changes: 82 additions & 1 deletion Localizer/Utils.cs → Localizer/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Harmony.ILCopying;
using Localizer.Attributes;
using Localizer.DataModel;
Expand All @@ -16,7 +18,7 @@

namespace Localizer
{
public static class Utils
public static partial class Utils
{
#region Zip

Expand Down Expand Up @@ -160,6 +162,45 @@ public static void LogInfo(object o)

#endregion

#region Network

public static HttpWebResponse GET(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 9000;

return (HttpWebResponse)request.GetResponse();
}

public static string GetResponseBody(HttpWebResponse response)
{
if (response == null)
return null;

Encoding encoding;
try
{
encoding = Encoding.GetEncoding(response.ContentEncoding);
}
catch (ArgumentException e)
{
encoding = Encoding.UTF8;
}

using (var myResponseStream = response.GetResponseStream())
{
using (var myStreamReader = new StreamReader(myResponseStream, encoding))
{
return myStreamReader.ReadToEnd();
}
}
}

#endregion

#region Others

/// <summary>
Expand Down Expand Up @@ -238,6 +279,46 @@ public static List<ILInstruction> GetInstructions(MethodInfo method)
var dummy = new DynamicMethod("Dummy", typeof(void), new Type[] { });
return MethodBodyReader.GetInstructions(dummy.GetILGenerator(), method);
}

public static void SafeWrap(Action action)
{
SafeWrap(action, out var ex);
}

public static void SafeWrap(Action action, out Exception ex)
{
try
{
ex = null;
action();
}
catch (Exception e)
{
ex = e;
LogError(e);
}
}

public static T SafeWrap<T>(Func<T> func)
{
return SafeWrap(func, out var ex);
}

public static T SafeWrap<T>(Func<T> func, out Exception ex)
{
try
{
ex = null;
return func();
}
catch (Exception e)
{
ex = e;
LogError(e);
}

return default;
}
#endregion
}
}
42 changes: 35 additions & 7 deletions Localizer/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using Localizer.Modules;
using Localizer.ServiceInterfaces.Network;
using Localizer.Services;
using log4net;
using Microsoft.Xna.Framework;
using MonoMod.RuntimeDetour.HookGen;
using Ninject;
using Ninject.Modules;
using Terraria;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.UI;

namespace Localizer
{
Expand All @@ -24,6 +31,10 @@ public Localizer()
{
_gameCultures =
typeof(GameCulture).GetFieldDirectly(null, "_legacyCultures") as Dictionary<int, GameCulture>;

Kernel = new LocalizerKernel();
Kernel.Bind<RefreshLanguageService>().To<RefreshLanguageService>().InSingletonScope();
Kernel.Get<RefreshLanguageService>();
}

public static ILog Log { get; private set; }
Expand All @@ -50,8 +61,6 @@ public override void Load()
Utils.CreateDirectory(DownloadPackageDirPath);

LoadConfig();

Kernel = new LocalizerKernel();
Kernel.Load(new NinjectModule[]
{
new DefaultPackageModule(), new DefaultFileExportModule(),
Expand All @@ -65,6 +74,25 @@ public override void PostSetupContent()
PluginManager.LoadPlugins();
}

public void CheckUpdate()
{
Task.Run(() =>
{
var curVersion = this.Version;
if (Kernel.Get<IUpdateService>().CheckUpdate(curVersion, out var updateInfo))
{
if (Main.gameMenu)
{

}
else
{
Main.NewText("New Version Detected!", Color.Red);
}
}
});
}

public override void Unload()
{
try
Expand Down Expand Up @@ -124,15 +152,15 @@ public static GameCulture CultureInfoToGameCulture(CultureInfo culture)
var gc = GameCulture.FromName(culture.Name);
return gc ?? AddGameCulture(culture);
}

public static void RefreshLanguages(CultureInfo lang)
public static void RefreshLanguages()
{
ModContent.RefreshModLanguage(CultureInfoToGameCulture(lang));
Kernel.Get<RefreshLanguageService>().Refresh();
}

public static void RefreshLanguages()
public static void CloseTmodFile()
{
ModContent.RefreshModLanguage(LanguageManager.Instance.ActiveCulture);

}
}
}
Loading

0 comments on commit a229f3f

Please sign in to comment.