diff --git a/ClassicalSharp/Blocks/BlockInfo.cs b/ClassicalSharp/Blocks/BlockInfo.cs index ca245e7fe..4bf5ce0ae 100644 --- a/ClassicalSharp/Blocks/BlockInfo.cs +++ b/ClassicalSharp/Blocks/BlockInfo.cs @@ -250,7 +250,6 @@ namespace ClassicalSharp { "_Gold_Iron_Double slab_Slab_Brick_TNT_Bookshelf_Mossy rocks_Obsidian_Cobblestone slab_Rope_Sandstone" + "_Snow_Fire_Light pink_Forest green_Brown_Deep blue_Turquoise_Ice_Ceramic tile_Magma_Pillar_Crate_Stone brick"; - static StringBuffer buffer = new StringBuffer(64); static string DefaultName(BlockID block) { if (block >= Block.CpeCount) return "Invalid"; @@ -258,14 +257,11 @@ namespace ClassicalSharp { int start = 0; for (int i = 0; i < block; i++) start = RawNames.IndexOf('_', start) + 1; + int end = RawNames.IndexOf('_', start); if (end == -1) end = RawNames.Length; - buffer.Clear(); - for (int i = start; i < end; i++) { - buffer.Append(RawNames[i]); - } - return buffer.ToString(); + return RawNames.Substring(start, end - start); } diff --git a/ClassicalSharp/Game/ChatLog.cs b/ClassicalSharp/Game/ChatLog.cs index 31a0da926..c23b0e3eb 100644 --- a/ClassicalSharp/Game/ChatLog.cs +++ b/ClassicalSharp/Game/ChatLog.cs @@ -96,7 +96,7 @@ namespace ClassicalSharp { StreamWriter writer = null; void LogChatToFile(string text) { if (logName == null || !game.ChatLogging) return; - DateTime now = DateTime.Now; + DateTime now = Utils.LocalNow(); if (now.Day != last.Day || now.Month != last.Month || now.Year != last.Year) { Dispose(); diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index a8c683b02..2c1766593 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -379,7 +379,7 @@ namespace ClassicalSharp { Platform.DirectoryCreate("screenshots"); } - string timestamp = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); + string timestamp = Utils.LocalNow().ToString("dd-MM-yyyy-HH-mm-ss"); string file = "screenshot_" + timestamp + ".png"; string path = PathIO.Combine("screenshots", file); diff --git a/ClassicalSharp/Network/Utils/AsyncDownloader.cs b/ClassicalSharp/Network/Utils/AsyncDownloader.cs index 6467e1ab5..f8c405c81 100644 --- a/ClassicalSharp/Network/Utils/AsyncDownloader.cs +++ b/ClassicalSharp/Network/Utils/AsyncDownloader.cs @@ -216,7 +216,7 @@ namespace ClassicalSharp.Network { static DateTime GetLastModified(HttpWebResponse response) { // System.NotSupportedException: Can't get timezone name. - // gets thrown on some platforms with DateTime.Now + // Gets thrown on some platforms with DateTime.Now try { if (response.Headers.Get("Last-Modified") != null) return response.LastModified; diff --git a/ClassicalSharp/Utils/ErrorHandler.cs b/ClassicalSharp/Utils/ErrorHandler.cs index 0c96ba362..f7a4e201d 100644 --- a/ClassicalSharp/Utils/ErrorHandler.cs +++ b/ClassicalSharp/Utils/ErrorHandler.cs @@ -47,7 +47,7 @@ namespace ClassicalSharp { using (StreamWriter w = new StreamWriter(fs)) { w.WriteLine("=== crash occurred ==="); - w.WriteLine("Time: " + DateTime.Now); + w.WriteLine("Time: " + Utils.LocalNow()); string platform = Configuration.RunningOnMono ? "Mono " : ".NET "; platform += Environment.Version; diff --git a/ClassicalSharp/Utils/Utils.cs b/ClassicalSharp/Utils/Utils.cs index 6f50b99da..cbd2cc22e 100644 --- a/ClassicalSharp/Utils/Utils.cs +++ b/ClassicalSharp/Utils/Utils.cs @@ -2,6 +2,8 @@ using System; using System.Drawing; using System.Globalization; +using System.Runtime.InteropServices; +using System.Security; #if !LAUNCHER using ClassicalSharp.Model; #endif @@ -29,6 +31,32 @@ namespace ClassicalSharp { public const int StringLength = 64; + [StructLayout(LayoutKind.Sequential, Pack=2)] + internal struct SYSTEMTIME { + public ushort Year, Month, DayOfWeek, Day; + public ushort Hour, Minute, Second, Millis; + } + + [DllImport("kernel32.dll"), SuppressUnmanagedCodeSecurity] + static extern void GetLocalTime(out SYSTEMTIME st); + + static DateTime LocalNow_Windows() { + SYSTEMTIME st; GetLocalTime(out st); + return new DateTime(st.Year, st.Month, st.Day, st.Hour, st.Minute, + st.Second, st.Millis, DateTimeKind.Local); + } + + public static DateTime LocalNow() { + // System.NotSupportedException: Can't get timezone name. + // Gets thrown on some platforms with DateTime.Now + try { + // avoid pinvoke-ing GetLocalTime function on non-windows OS + if (OpenTK.Configuration.RunningOnWindows) return LocalNow_Windows(); + } catch { } + return DateTime.Now; + } + + public static string StripColours(string value) { if (value.IndexOf('&') == -1) return value; char[] output = new char[value.Length]; diff --git a/Launcher2/Patcher/ZipWriter.cs b/Launcher2/Patcher/ZipWriter.cs index 009437102..d4afcceab 100644 --- a/Launcher2/Patcher/ZipWriter.cs +++ b/Launcher2/Patcher/ZipWriter.cs @@ -18,7 +18,7 @@ namespace Launcher.Patcher { public ZipWriter(Stream stream) { this.stream = stream; writer = new BinaryWriter(stream); - now = DateTime.Now; + now = Utils.LocalNow(); } internal ZipEntry[] entries;