Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Progress on the level loading procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
REHERC committed Jul 21, 2021
1 parent 64cbb60 commit 7a8c094
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 35 deletions.
13 changes: 10 additions & 3 deletions Common.Distance.AdventureMaker/Utilities/CampaignFile.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
using Distance.AdventureMaker.Common.Enums;
using Distance.AdventureMaker.Common.Models.Resources;
using System.Collections.Generic;
using System.Linq;

namespace Distance.AdventureMaker.Common.Models
{
public partial class CampaignFile
{
public CampaignResource GetResource(string guid, ResourceType type)
{
public CampaignResource GetResource(string guid, ResourceType type)
{
return Data.Resources.Find((res) => Equals(res.guid, guid) && Equals(res.resource_type, type));
}
}

public IEnumerable<CampaignResource> GetResources(ResourceType type)
{
return Data.Resources.Where(x => x.resource_type == type);
}
}
}
42 changes: 42 additions & 0 deletions Distance.AdventureMaker/DataModel/LevelData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.IO;
using UnityEngine;

namespace Distance.AdventureMaker.DataModel
{
public class LevelData
{
public readonly string alias;
public readonly FileInfo levelFile;
public readonly FileInfo thumbnailFile;

private Texture2D thumbnailTexture_;
public Texture2D ThumbnailTexture
{
get
{
if (!thumbnailTexture_ && thumbnailFile?.Exists == true)
{
thumbnailTexture_ = Resource.LoadTextureFromFile(thumbnailFile.FullName, 640, 360) as Texture2D;
}

return thumbnailTexture_;
}
}

public LevelData(string alias, string level, string thumbnail = null) : this(alias, new FileInfo(level), new FileInfo(thumbnail))
{
}

public LevelData(string alias, FileInfo level, FileInfo thumbnail = null)
{
this.alias = alias;
levelFile = level;
thumbnailFile = thumbnail;
}

public override int GetHashCode()
{
return levelFile.FullName.GetHashCode();
}
}
}
7 changes: 7 additions & 0 deletions Distance.AdventureMaker/Distance.AdventureMaker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationArguments.cs" />
<Compile Include="DataModel\LevelData.cs" />
<Compile Include="Extensions\mscorlib\System\StringEx.cs" />
<Compile Include="Harmony\Assembly-CSharp\MainMenuLogic\Awake.cs" />
<Compile Include="Harmony\Assembly-CSharp\SteamworksUGC\OnEnable.cs" />
<Compile Include="Interfaces\ILevelRegister.cs" />
<Compile Include="Scripts\CustomMenus\CampaignSelectMenuLogic.cs" />
<Compile Include="Scripts\Loader\CampaignLoaderLogic.cs" />
<Compile Include="Entry.cs" />
Expand All @@ -110,6 +113,7 @@
<Compile Include="Scripts\MainMenu\Buttons\MainMenuButton.cs" />
<Compile Include="Scripts\MainMenu\Buttons\ContentPacksButton.cs" />
<Compile Include="Scripts\MainMenu\MainMenuButtonController.cs" />
<Compile Include="Scripts\Managers\Campaigns\LevelRegister.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand All @@ -121,6 +125,9 @@
<Name>Centrifuge.Distance</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Scripts\Managers\Campaigns\CampaignManager.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="..\Common.Distance.AdventureMaker\Common.Distance.AdventureMaker.projitems" Label="Shared" />
<Import Project="..\Common.Json\Common.Json.projitems" Label="Shared" />
Expand Down
10 changes: 7 additions & 3 deletions Distance.AdventureMaker/Entry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Distance.AdventureMaker.Loader;
using Distance.AdventureMaker.Scripts.Managers.Campaigns;
using Events.MainMenu;
using Reactor.API.Attributes;
using Reactor.API.Interfaces.Systems;
Expand All @@ -13,14 +14,16 @@ public class Mod : MonoBehaviour
{
public static Mod Instance;

public IManager Manager { get; set; }
public IManager Manager { get; private set; }

public Log Logger { get; set; }
public Log Logger { get; private set; }

public ApplicationArguments StartParameters { get; set; }
public ApplicationArguments StartParameters { get; private set; }

private CampaignLoaderLogic CampaignLoader { get; set; }

internal CampaignManager CampaignManager { get; private set; }

public void Initialize(IManager manager)
{
DontDestroyOnLoad(this);
Expand All @@ -30,6 +33,7 @@ public void Initialize(IManager manager)
Logger = LogManager.GetForCurrentAssembly();

CampaignLoader = gameObject.AddComponent<CampaignLoaderLogic>();
CampaignManager = gameObject.AddComponent<CampaignManager>();

StartParameters = ApplicationArguments.Parse();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public static class StringExtensions
{
public static string NormalizePath(this string input)
{
return input.Normalize().Replace('\\', '/');
}
}
22 changes: 22 additions & 0 deletions Distance.AdventureMaker/Interfaces/ILevelRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.IO;
using UnityEngine;

namespace Distance.AdventureMaker.Interfaces
{
public interface ILevelRegister
{
void RegisterLevel(string alias, FileInfo bytesFile, FileInfo thumbnailTexture = null);

void RegisterLevel(string alias, string bytesFile, string thumbnailTexture = null);

string GetLevelAlias(string levelPath);

string GetLevelPath(string alias);

Texture2D GetThumbnailFromPath(FileInfo levelFile);

Texture2D GetThumbnailFromPath(string levelPath);

Texture2D GetThumbnailFromAlias(string alias);
}
}
26 changes: 21 additions & 5 deletions Distance.AdventureMaker/Scripts/Loader/Steps/CampaignExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,23 @@

namespace Distance.AdventureMaker.Loader.Steps
{
public class CampaignExtractor : LoaderTask
public class CampaignExtractor : LoaderTask, IEnumerable<DirectoryInfo>
{
private List<DirectoryInfo> directories;

public CampaignExtractor(CampaignLoader loader) : base(loader)
{
directories = new List<DirectoryInfo>();
}

public IEnumerator<DirectoryInfo> GetEnumerator()
{
return new HashSet<string>(directories.Where(x => x.Exists).Select(x => Path.GetFullPath(x.FullName).ToLower())).Select(x => new DirectoryInfo(x)).GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public override IEnumerator Run(Task.Status status)
Expand All @@ -23,9 +36,11 @@ public override IEnumerator Run(Task.Status status)
CampaignListing.CampaignItem data;
FileInfo archivePath;

directories.AddRange(loader.Listing.Folders.Select(x => x.Value.path as DirectoryInfo));

foreach (var item in loader.Listing.Archives)
{
Mod.Instance.Logger.Info($"[{item.Value.source}]\t {item.Key} : {item.Value.path.FullName}");
//Mod.Instance.Logger.Info($"[{item.Value.source}]\t {item.Key} : {item.Value.path.FullName}");

id = item.Key;
data = item.Value;
Expand Down Expand Up @@ -68,13 +83,14 @@ public override IEnumerator Run(Task.Status status)
string entryPath = SanitizeFileName(entry.Key, Path.GetInvalidPathChars);
destination = new FileInfo(Path.Combine(extractTo.FullName, entryPath));

Mod.Instance.Logger.Warning(destination.FullName);
//Mod.Instance.Logger.Warning(destination.FullName);

destination.Directory.CreateIfDoesntExist();

using (Stream fileStream = File.Open(destination.FullName, FileMode.Create))
{
status.SetText($"Extracting {item.Value.path.FullName} ...\n{entry.Key}");
//Mod.Instance.Logger.Error($"{destination.FullName}");

entry.Value.WriteTo(fileStream);
fileStream.Flush();
Expand All @@ -83,9 +99,9 @@ public override IEnumerator Run(Task.Status status)
}
}
}
}

yield break;
directories.Add(extractTo);
}
}

protected string SanitizeFileName(string id, Func<char[]> invalidCharsCallback)
Expand Down
45 changes: 21 additions & 24 deletions Distance.AdventureMaker/Scripts/Loader/Steps/CampaignImporter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Centrifuge.Distance.Game;
using System;
using Distance.AdventureMaker.Common.Enums;
using Distance.AdventureMaker.Common.Models;
using Distance.AdventureMaker.Common.Models.Resources;
using System.Collections;
using System.Text;
using UnityEngine;
using System.IO;
using static Distance.AdventureMaker.Loader.CampaignLoaderLogic;

namespace Distance.AdventureMaker.Loader.Steps
Expand All @@ -16,33 +17,29 @@ public CampaignImporter(CampaignLoader loader) : base(loader)
public override IEnumerator Run(Task.Status status)
{
yield break;
status.SetText("Loading campaigns...");

/*
DiscordRpc.RichPresence rpc = new DiscordRpc.RichPresence();
rpc.details = "Waiting...";
rpc.state = "In Main Menu";
rpc.startTimestamp = 0L;
rpc.endTimestamp = 0L;
rpc.largeImageKey = "official_level";
TimeSpan span;
StringBuilder dots;
double totalTime = 0;
while (true)
foreach (DirectoryInfo campaignPath in loader.Extractor)
{
dots = new StringBuilder();
dots.Append('.', 1 + ((int)(totalTime * 2) % 3));
CampaignFile campaign = Json.Load<CampaignFile>(Path.Combine(campaignPath.FullName, "project.json"));
//Mod.Instance.Logger.Warning($"{campaign.Metadata.Title} by {campaign.Metadata.Author}");

totalTime += Time.deltaTime;
foreach (var resource in campaign.GetResources(ResourceType.Level))
{
CampaignResource.Level level = resource as CampaignResource.Level;

span = TimeSpan.FromSeconds(totalTime);
status.SetText($"You have been waiting for {Colors.red.ToFormattedHex()}{span.Hours:000}:{span.Minutes:00}:{span.Seconds:00}[/c][-] {dots}\n{Colors.gray.ToFormattedHex()}There is nothing of interest here (really)");
status.SetProgress((uint)span.Milliseconds, 1000);
string levelAlias = $"ModdedLevels/{campaign.Metadata.Guid}/{level.guid}.bytes";
string bytesPath = GetResourceFullPath(campaignPath, level.file);
string thumbPath = GetResourceFullPath(campaignPath, level.thumbnail);

DiscordRpc.UpdatePresence(ref rpc);
yield return null;
Mod.Instance.CampaignManager.Levels.RegisterLevel(levelAlias, bytesPath, thumbPath);
}
}
*/
}

protected string GetResourceFullPath(DirectoryInfo campaignRoot, string file)
{
return Path.Combine(campaignRoot.FullName, "resources/" + file);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Distance.AdventureMaker.Interfaces;
using System.IO;
using UnityEngine;

namespace Distance.AdventureMaker.Scripts.Managers.Campaigns
{
public class CampaignManager : MonoBehaviour
{
public ILevelRegister Levels { get; private set; }

public void Awake()
{
Levels = new LevelRegister();
}


}
}
Loading

0 comments on commit 7a8c094

Please sign in to comment.