Work on architecture. A lot of refractoring. Updated to unity 5.
This commit is contained in:
@ -8,7 +8,7 @@ namespace TransportGame.Utils
|
||||
{
|
||||
public static class Logger
|
||||
{
|
||||
private static StreamWriter logFile;
|
||||
private static string logFile = null;
|
||||
|
||||
public enum Level
|
||||
{
|
||||
@ -23,28 +23,40 @@ namespace TransportGame.Utils
|
||||
// Open log file if not opened
|
||||
if (logFile == null)
|
||||
{
|
||||
logFile = new StreamWriter(String.Format("Logs\\{0}.log", DateTime.Now.Ticks));
|
||||
// Create logs folder
|
||||
if (!Directory.Exists("Logs"))
|
||||
Directory.CreateDirectory("Logs");
|
||||
|
||||
// Create log file
|
||||
logFile = String.Format("Logs\\{0}.log", DateTime.Now.Ticks);
|
||||
}
|
||||
|
||||
// Log to file
|
||||
logFile.Write("[{0}] ", Enum.GetName(typeof(Level), level));
|
||||
logFile.Write(DateTime.Now.ToLongTimeString());
|
||||
logFile.WriteLine(": " + format, args);
|
||||
|
||||
// Log to unity
|
||||
switch (level)
|
||||
lock (logFile)
|
||||
{
|
||||
case Level.Warning:
|
||||
UnityEngine.Debug.LogWarning(String.Format(format, args));
|
||||
break;
|
||||
using (var writer = new StreamWriter(logFile, true))
|
||||
{
|
||||
// Log to file
|
||||
writer.Write("[{0}] ", Enum.GetName(typeof(Level), level));
|
||||
writer.Write(DateTime.Now.ToString("%HH:%mm:%ss.%FFF"));
|
||||
writer.WriteLine(": " + format, args);
|
||||
writer.Close();
|
||||
}
|
||||
|
||||
case Level.Error:
|
||||
UnityEngine.Debug.LogError(String.Format(format, args));
|
||||
break;
|
||||
// Log to unity
|
||||
switch (level)
|
||||
{
|
||||
case Level.Warning:
|
||||
UnityEngine.Debug.LogWarning(String.Format(format, args));
|
||||
break;
|
||||
|
||||
case Level.Critical:
|
||||
UnityEngine.Debug.LogError(String.Format(format, args));
|
||||
break;
|
||||
case Level.Error:
|
||||
UnityEngine.Debug.LogError(String.Format(format, args));
|
||||
break;
|
||||
|
||||
case Level.Critical:
|
||||
UnityEngine.Debug.LogError(String.Format(format, args));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
56
Game/Assets/Scripts/Utils/Task.cs
Normal file
56
Game/Assets/Scripts/Utils/Task.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class Task
|
||||
{
|
||||
private class TaskInfo
|
||||
{
|
||||
public Action Action { get; set; }
|
||||
public bool? Success { get; set; }
|
||||
public Exception ThrownException { get; set; }
|
||||
}
|
||||
|
||||
private static void RunAsync_ActionThread(object info)
|
||||
{
|
||||
TaskInfo taskInfo = (TaskInfo)info;
|
||||
|
||||
try
|
||||
{
|
||||
taskInfo.Action();
|
||||
taskInfo.Success = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
taskInfo.ThrownException = ex;
|
||||
taskInfo.Success = false;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable RunAsync(Action action)
|
||||
{
|
||||
// Set up task info object
|
||||
TaskInfo taskInfo = new TaskInfo();
|
||||
taskInfo.Action = action;
|
||||
|
||||
// Start thread and wait
|
||||
var thread = new Thread(RunAsync_ActionThread);
|
||||
thread.Start(taskInfo);
|
||||
|
||||
// Wait for thread to finish
|
||||
while (thread.ThreadState == ThreadState.Running)
|
||||
yield return null;
|
||||
|
||||
thread.Join();
|
||||
|
||||
// Rethrow exception
|
||||
if (taskInfo.Success.HasValue && !taskInfo.Success.Value)
|
||||
throw new Exception("Task failed", taskInfo.ThrownException);
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Utils/Task.cs.meta
Normal file
12
Game/Assets/Scripts/Utils/Task.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e07530e47d7019b418499d12a7c29f35
|
||||
timeCreated: 1425923206
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
25
Game/Assets/Scripts/Utils/XmlHelper.cs
Normal file
25
Game/Assets/Scripts/Utils/XmlHelper.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class XmlHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Deserializes a file
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to deserialize</typeparam>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <returns>Deserialized object</returns>
|
||||
public static T Deserialize<T>(string filename)
|
||||
{
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
var stream = new StreamReader(filename);
|
||||
return (T)serializer.Deserialize(stream);
|
||||
}
|
||||
}
|
||||
}
|
12
Game/Assets/Scripts/Utils/XmlHelper.cs.meta
Normal file
12
Game/Assets/Scripts/Utils/XmlHelper.cs.meta
Normal file
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4d12846fa3e22f4fbd829e42582d6a9
|
||||
timeCreated: 1425647029
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user