Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HeleusCore committed Feb 21, 2020
0 parents commit 4a83c91
Show file tree
Hide file tree
Showing 117 changed files with 15,417 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
@@ -0,0 +1,6 @@
bin/
obj/
packages/
heleusdata*/
.vs/
.vscode/
44 changes: 44 additions & 0 deletions Base/ConcurrentLoader.cs
@@ -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;
}
}
}
54 changes: 54 additions & 0 deletions Base/Config.cs
@@ -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));
}
}
}
54 changes: 54 additions & 0 deletions Base/Json.cs
@@ -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);
}
}
}
14 changes: 14 additions & 0 deletions Base/Mth.cs
@@ -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);
}
}
}
21 changes: 21 additions & 0 deletions Chain/Blocks/BlockEvent.cs
@@ -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;
}
}
}
6 changes: 6 additions & 0 deletions Chain/Blocks/BlockGenerator.cs
@@ -0,0 +1,6 @@
namespace Heleus.Chain.Blocks
{
public class BlockGenerator
{
}
}

0 comments on commit 4a83c91

Please sign in to comment.