Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4a83c91
Showing
117 changed files
with
15,417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bin/ | ||
obj/ | ||
packages/ | ||
heleusdata*/ | ||
.vs/ | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Heleus.Base | ||
{ | ||
public class ConcurrentLoader<TKey, TType> | ||
{ | ||
readonly SemaphoreSlim _sem = new SemaphoreSlim(1); | ||
readonly Func<TKey, Task<TType>> _query; | ||
readonly Func<TKey, Task<TType>> _store; | ||
|
||
public ConcurrentLoader(Func<TKey, Task<TType>> query, Func<TKey, Task<TType>> store) | ||
{ | ||
_query = query; | ||
_store = store; | ||
} | ||
|
||
public async Task<TType> Get(TKey key) | ||
{ | ||
var data = await _query.Invoke(key); | ||
if (!data.IsNullOrDefault()) | ||
return data; | ||
|
||
await _sem.WaitAsync(); | ||
try | ||
{ | ||
data = await _query.Invoke(key); | ||
if (!data.IsNullOrDefault()) | ||
return data; | ||
|
||
data = await _store.Invoke(key); | ||
} | ||
catch(Exception ex) | ||
{ | ||
Log.IgnoreException(ex); | ||
} | ||
|
||
_sem.Release(); | ||
return data; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
|
||
namespace Heleus.Base | ||
{ | ||
public class Config | ||
{ | ||
string _fileName; | ||
Storage _storage; | ||
|
||
protected virtual void Loaded() | ||
{ | ||
|
||
} | ||
|
||
public static T Load<T>(Storage storage, bool createEmpty = true) where T : Config, new() | ||
{ | ||
var _config = default(T); | ||
var fileName = typeof(T).Name.ToLower() + ".txt"; | ||
|
||
var json = storage.ReadFileText(fileName); | ||
if (json == null && !createEmpty) | ||
return _config; | ||
|
||
var error = false; | ||
try | ||
{ | ||
if (!string.IsNullOrEmpty(json)) | ||
_config = Json.ToObject<T>(json); | ||
} | ||
catch(Exception ex) | ||
{ | ||
Log.Error($"Parsing {fileName} failed: {ex}"); | ||
error = true; | ||
} | ||
|
||
if (_config == null) | ||
_config = new T(); | ||
|
||
_config._fileName = fileName; | ||
_config._storage = storage; | ||
_config.Loaded(); | ||
|
||
if(!error) // don't override on error | ||
Save(_config); | ||
|
||
return _config; | ||
} | ||
|
||
public static void Save(Config config) | ||
{ | ||
config._storage.WriteFileText(config._fileName, Json.ToNiceJson(config)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace Heleus.Base | ||
{ | ||
class WriteableOnlyContractResolver : DefaultContractResolver | ||
{ | ||
readonly HashSet<string> _ignoredProperties = new HashSet<string>(); | ||
|
||
public WriteableOnlyContractResolver(string[] ignoreList = null) | ||
{ | ||
if (ignoreList != null) | ||
_ignoredProperties = new HashSet<string>(ignoreList); | ||
} | ||
|
||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) | ||
{ | ||
var properties = base.CreateProperties(type, memberSerialization); | ||
properties = properties.Where(p => p.Writable && !_ignoredProperties.Contains(p.PropertyName)).ToList(); | ||
return properties; | ||
} | ||
} | ||
|
||
public static class Json | ||
{ | ||
public static string ToJson(object data) | ||
{ | ||
return JsonConvert.SerializeObject(data); | ||
} | ||
|
||
public static string ToNiceJson(object data) | ||
{ | ||
return JsonConvert.SerializeObject(data, Formatting.Indented); | ||
} | ||
|
||
public static string ToJson(object data, params string[] ignoreProperties) | ||
{ | ||
return JsonConvert.SerializeObject(data, Formatting.None, new JsonSerializerSettings { ContractResolver = new WriteableOnlyContractResolver(ignoreProperties) }); | ||
} | ||
|
||
public static T ToObject<T>(string json) | ||
{ | ||
return JsonConvert.DeserializeObject<T>(json); | ||
} | ||
|
||
public static object ToObject(Type type, string json) | ||
{ | ||
return JsonConvert.DeserializeObject(json, type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
namespace Heleus.Base | ||
{ | ||
public static class Mth | ||
{ | ||
public static float Percentage(int value, int total) | ||
{ | ||
if (total <= 0) | ||
return 0; | ||
|
||
return ((100f * value) / total); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
namespace Heleus.Chain.Blocks | ||
{ | ||
public class BlockEvent<BlockType> where BlockType : Block | ||
{ | ||
public readonly BlockType Block; | ||
public readonly BlockSignatures BlockSignature; | ||
|
||
public BlockEvent(BlockType block, BlockSignatures blockSignature) | ||
{ | ||
Block = block; | ||
BlockSignature = blockSignature; | ||
} | ||
|
||
public BlockEvent(BlockData<BlockType> blockData) | ||
{ | ||
Block = blockData.Block; | ||
BlockSignature = blockData.Signatures; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Heleus.Chain.Blocks | ||
{ | ||
public class BlockGenerator | ||
{ | ||
} | ||
} |
Oops, something went wrong.