Skip to content

Commit

Permalink
Mud Namespace, Systems, Tables structure, base classes, extensions in…
Browse files Browse the repository at this point in the history
…cluding registration, batching, resources and reorganisation common into world, store
  • Loading branch information
juanfranblanco committed May 14, 2024
1 parent 0c7836a commit 8c493f4
Show file tree
Hide file tree
Showing 127 changed files with 2,629 additions and 1,166 deletions.
2 changes: 1 addition & 1 deletion buildConf/Version.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionMajor>4</VersionMajor>
<VersionMinor>20</VersionMinor>
<VersionMinor>21</VersionMinor>
<VersionPatch>0</VersionPatch>
<NonDevAssemblyVersion>$(VersionMajor).$(VersionMinor).0</NonDevAssemblyVersion>
<AssemblyVersion>$(NonDevAssemblyVersion)</AssemblyVersion>
Expand Down
20 changes: 20 additions & 0 deletions src/Nethereum.Mud.Contracts/Core/Namespaces/NamespaceBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Nethereum.Mud.Contracts.Core.Systems;
using Nethereum.Mud.Contracts.Core.Tables;

namespace Nethereum.Mud.Contracts.Core.Namespaces
{
public class NamespaceBase<TNamespaceResource, TSystemServices, TTableServices> where TNamespaceResource : NamespaceResource
where TSystemServices : SystemsServices
where TTableServices : TablesServices
{
public NamespaceBase(TNamespaceResource namespaceResource)
{
NamespaceResource = namespaceResource;
}

public TNamespaceResource NamespaceResource { get; protected set; }

public TSystemServices Systems { get; protected set; }
public TTableServices Tables { get; protected set; }
}
}
23 changes: 23 additions & 0 deletions src/Nethereum.Mud.Contracts/Core/Namespaces/UserNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Nethereum.Web3;
using Nethereum.Mud.Contracts.Core.Systems;
using Nethereum.Mud.Contracts.World;
using Nethereum.Mud.Contracts.Store;
using Nethereum.Mud.Contracts.Core.Tables;

namespace Nethereum.Mud.Contracts.Core.Namespaces
{
public class UserNamespace<TNamespaceResource, TSystemServices, TTableServices> : NamespaceBase<TNamespaceResource, TSystemServices, TTableServices>
where TNamespaceResource : NamespaceResource, new()
where TSystemServices : SystemsServices
where TTableServices : TablesServices
{
public WorldNamespace World { get; protected set; }
public StoreNamespace Store { get; protected set; }
public UserNamespace(IWeb3 web3, string contractAddress) : base(new TNamespaceResource())
{
World = new WorldNamespace(web3, contractAddress);
Store = new StoreNamespace(web3, contractAddress);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.ABI.FunctionEncoding.Attributes;

namespace Nethereum.Mud.Contracts.StoreEvents
namespace Nethereum.Mud.Contracts.Core.StoreEvents
{
public partial class StoreDeleteRecordEventDTO : StoreDeleteRecordEventDTOBase { }

Expand Down Expand Up @@ -50,7 +50,7 @@ public class StoreSetRecordEventDTOBase : IEventDTO
public virtual byte[] DynamicData { get; set; }
}


public partial class StoreSpliceDynamicDataEventDTO : StoreSpliceDynamicDataEventDTOBase { }
[Event("Store_SpliceDynamicData")]
public class StoreSpliceDynamicDataEventDTOBase : IEventDTO
Expand Down

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/Nethereum.Mud.Contracts/Core/Systems/EmptySystemsServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Nethereum.Web3;
using System.Collections.Generic;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public class EmptySystemsServices : SystemsServices
{
public EmptySystemsServices(IWeb3 web3, string contractAddress) : base(web3, contractAddress)
{
SystemServices = new List<ISystemService>();
}
}
}
20 changes: 20 additions & 0 deletions src/Nethereum.Mud.Contracts/Core/Systems/ISystemService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Nethereum.ABI.Model;
using Nethereum.Contracts;
using System.Collections.Generic;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public interface ISystemService<TSystemResource>: ISystemService where TSystemResource : SystemResource
{

}


public interface ISystemService
{
public List<FunctionABI> GetSystemFunctionABIs();
public IResource Resource { get; }

public ISystemServiceResourceRegistration SystemServiceResourceRegistrator { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Nethereum.Mud.Contracts.World.Systems.BatchCallSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Tables;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public interface ISystemServiceResourceRegistration
{
Task<string> BatchRegisterRootFunctionSelectorsRequestAsync(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true);
Task<string> BatchRegisterSystemAndRootFunctionSelectorsRequestAsync(string deployedAddress, bool publicAccess = true, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true);
List<RegisterRootFunctionSelectorFunction> CreateRegisterRootFunctionSelectors(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true);
List<SystemCallData> CreateRegisterRootFunctionSelectorsBatchSystemCallData(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true);
List<SystemCallData> CreateRegisterSystemAndRegisterRootFunctionSelectorsBatchSystemCallData(string deployedAddress, bool publicAccess, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords, bool excludeDefaultSystemFunctions);
RegisterSystemFunction GetRegisterSystemFunction(string deployedAddress, bool publicAccess = true);
Task<string> RegisterSystemAsync(string deployedAddress, bool publicAccess = true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
using Nethereum.ABI.Model;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem.ContractDefinition;
using Nethereum.Hex.HexConvertors.Extensions;

namespace Nethereum.Mud.Contracts
namespace Nethereum.Mud.Contracts.Core.Systems
{


public class SystemDefaultFunctions
{
public List<Type> GetAllFunctionTypes()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Nethereum.ABI.Model;
using Nethereum.Contracts;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Mud.Contracts.World.Systems.BatchCallSystem;
using Nethereum.Mud.Contracts.World.Systems.BatchCallSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Tables;
using Nethereum.Web3;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public class SystemRegistrationBuilder
{
public List<RegisterRootFunctionSelectorFunction> CreateRegisterRootFunctionSelectors<TSystemResource>(ContractServiceBase systemService, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
where TSystemResource : SystemResource, new()
{
return CreateRegisterFunctionSelectors<TSystemResource>(systemService.GetAllFunctionABIs(), excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}

public List<RegisterRootFunctionSelectorFunction> CreateRegisterRootFunctionSelectors<TSystemResource>(ISystemService<TSystemResource> systemService, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
where TSystemResource : SystemResource, new()
{
return CreateRegisterRootFunctionSelectors(systemService.GetSystemFunctionABIs(), systemService.GetResource().ResourceIdEncoded, excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}


public List<RegisterRootFunctionSelectorFunction> CreateRegisterFunctionSelectors(List<FunctionABI> functionABIs, Type systemResourceType, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
return CreateRegisterRootFunctionSelectors(functionABIs, ResourceRegistry.GetResourceEncoded(systemResourceType), excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}

public List<RegisterRootFunctionSelectorFunction> CreateRegisterRootFunctionSelectors(List<FunctionABI> functionABIs, byte[] systemResourceId, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
if (excludedFunctionSelectorRecords == null)
{
excludedFunctionSelectorRecords = new List<FunctionSelectorsTableRecord>();
}
var excludedRegisteredSelectors = excludedFunctionSelectorRecords.Select(x => x.Values.SystemFunctionSelector.ToString()).ToList();
if (excludeDefaultSystemFunctions)
{
excludedRegisteredSelectors.AddRange(new SystemDefaultFunctions().GetAllFunctionSignatures().ToList());
}
excludedRegisteredSelectors.AddRange(new SystemDefaultFunctions().GetAllFunctionSignatures().ToList());
var functionAbis = functionABIs;
var functionSelectorsToRegister = functionAbis.Where(x => !excludedRegisteredSelectors.Any(y => y.IsTheSameHex(x.Sha3Signature))).ToList();

var registerFunctionSelectors = new List<RegisterRootFunctionSelectorFunction>();
foreach (var functionSelectorToRegister in functionSelectorsToRegister)
{
var registerFunction = new RegisterRootFunctionSelectorFunction();
registerFunction.SystemFunctionSignature = functionSelectorToRegister.Signature;
registerFunction.WorldFunctionSignature = functionSelectorToRegister.Signature;
registerFunction.SystemId = systemResourceId;
}
return registerFunctionSelectors;
}

public List<RegisterRootFunctionSelectorFunction> CreateRegisterFunctionSelectors<TSystemResource>(List<FunctionABI> functionABIs, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
where TSystemResource : SystemResource, new()
{

return CreateRegisterFunctionSelectors(functionABIs, typeof(TSystemResource), excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}

public RegisterSystemFunction CreateRegisterSystemFunction<TSystemResource, TService>(SystemServiceResourceRegistration<TSystemResource, TService> systemServiceResourceRegistration, string deployedAddress = null, bool publicAccess = true)
where TSystemResource : SystemResource, new()
where TService : ContractWeb3ServiceBase, ISystemService<TSystemResource>
{
var registerSystemFunction = new RegisterSystemFunction();
registerSystemFunction.SystemId = systemServiceResourceRegistration.Resource.ResourceIdEncoded;
registerSystemFunction.System = deployedAddress;
registerSystemFunction.PublicAccess = true;
return registerSystemFunction;
}

public List<SystemCallData> CreateRegisterRootFunctionSelectorsBatchSystemCallData<TSystemResource>(ISystemService<TSystemResource> systemService,
List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null,
bool excludeDefaultSystemFunctions = true)
where TSystemResource : SystemResource, new()
{

var registerFunctionSelectors = CreateRegisterRootFunctionSelectors(systemService, excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
return registerFunctionSelectors.CreateBatchSystemCallData<RegistrationSystemResource, RegisterRootFunctionSelectorFunction>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Nethereum.Web3;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public static class SystemResourceExtensions
{
public static TSystemResource GetResource<TSystemResource>(this ISystemService<TSystemResource> service) where TSystemResource : SystemResource, new()
{
return ResourceRegistry.GetResource<TSystemResource>();
}

public static SystemServiceResourceRegistration<TSystemResource, TService> GetSystemServiceResourceRegistration<TSystemResource, TService>(this TService service) where TSystemResource : SystemResource, new() where TService : ContractWeb3ServiceBase, ISystemService<TSystemResource>
{
return new SystemServiceResourceRegistration<TSystemResource, TService>(service);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Nethereum.Web3;
using Nethereum.Mud.Contracts.World.Systems.BatchCallSystem;
using Nethereum.Mud.Contracts.World.Systems.BatchCallSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem.ContractDefinition;
using Nethereum.Mud.Contracts.World.Systems.RegistrationSystem;
using Nethereum.Mud.Contracts.World.Tables;

namespace Nethereum.Mud.Contracts.Core.Systems
{
public class SystemServiceResourceRegistration<TSystemResource, TService> : ISystemServiceResourceRegistration where TSystemResource : SystemResource, new()
where TService : ContractWeb3ServiceBase, ISystemService<TSystemResource>
{
public TService Service { get; set; }
public string WorldAddress { get; }

protected SystemRegistrationBuilder systemRegistrationBuilder;

protected RegistrationSystemService RegistrationSystemService { get; set; }
protected BatchCallSystemService BatchCallSystemService { get; set; }

public SystemServiceResourceRegistration(TService service, string worldAddress)
{
Service = service;
WorldAddress = worldAddress;
InitialiseServices();
systemRegistrationBuilder = new SystemRegistrationBuilder();
}

private void InitialiseServices()
{
RegistrationSystemService = new RegistrationSystemService(Service.Web3, WorldAddress);
BatchCallSystemService = new BatchCallSystemService(Service.Web3, WorldAddress);
}

public SystemServiceResourceRegistration(TService service, string worldAddress, SystemRegistrationBuilder systemRegistrationBuilder)
{
Service = service;
WorldAddress = worldAddress;
InitialiseServices();
this.systemRegistrationBuilder = systemRegistrationBuilder;
}

public SystemServiceResourceRegistration(TService service)
{
Service = service;
WorldAddress = service.ContractAddress;
InitialiseServices();
systemRegistrationBuilder = new SystemRegistrationBuilder();
}



protected TSystemResource systemResource;

public TSystemResource Resource
{
get { if (systemResource == null) systemResource = new TSystemResource(); return systemResource; }
set { systemResource = value; }
}

public List<RegisterRootFunctionSelectorFunction> CreateRegisterRootFunctionSelectors(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
return systemRegistrationBuilder.CreateRegisterRootFunctionSelectors(Service, excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}

public List<SystemCallData> CreateRegisterRootFunctionSelectorsBatchSystemCallData(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
return systemRegistrationBuilder.CreateRegisterRootFunctionSelectorsBatchSystemCallData(Service, excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
}

public RegisterSystemFunction GetRegisterSystemFunction(string deployedAddress, bool publicAccess = true)
{
return systemRegistrationBuilder.CreateRegisterSystemFunction(this, deployedAddress, publicAccess);
}


public Task<string> RegisterSystemAsync(string deployedAddress, bool publicAccess = true)
{
var registerFunction = GetRegisterSystemFunction(deployedAddress, publicAccess);
return RegistrationSystemService.RegisterSystemRequestAsync(registerFunction);
}

public Task<string> BatchRegisterRootFunctionSelectorsRequestAsync(List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
var callData = CreateRegisterRootFunctionSelectorsBatchSystemCallData(excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
return BatchCallSystemService.BatchCallRequestAsync(callData);
}

public Task<string> BatchRegisterSystemAndRootFunctionSelectorsRequestAsync(string deployedAddress, bool publicAccess = true, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords = null, bool excludeDefaultSystemFunctions = true)
{
List<SystemCallData> registerFunctionBatchCallData = CreateRegisterSystemAndRegisterRootFunctionSelectorsBatchSystemCallData(deployedAddress, publicAccess, excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
return BatchCallSystemService.BatchCallRequestAsync(registerFunctionBatchCallData);
}

public List<SystemCallData> CreateRegisterSystemAndRegisterRootFunctionSelectorsBatchSystemCallData(string deployedAddress, bool publicAccess, List<FunctionSelectorsTableRecord> excludedFunctionSelectorRecords, bool excludeDefaultSystemFunctions)
{
var registerFunction = GetRegisterSystemFunction(deployedAddress, publicAccess);
var registerFunctionBatchCallData = new List<SystemCallData> { registerFunction.CreateBatchSystemCallDataForFunction<RegistrationSystemResource, RegisterSystemFunction>() };
var registerFunctionSelectors = CreateRegisterRootFunctionSelectorsBatchSystemCallData(excludedFunctionSelectorRecords, excludeDefaultSystemFunctions);
registerFunctionBatchCallData.AddRange(registerFunctionSelectors);
return registerFunctionBatchCallData;
}
}


}

0 comments on commit 8c493f4

Please sign in to comment.