99 lines
2.4 KiB
C#
99 lines
2.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
public class CommandSaveHandler
|
|
{
|
|
private static CommandSaveHandler instance;
|
|
|
|
private LinkedList<CommandDTO> commandList = new LinkedList<CommandDTO>();
|
|
private string saveFilePath = Path.Combine(Application.persistentDataPath, "commands.json");
|
|
|
|
public static CommandSaveHandler getInstance()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = new CommandSaveHandler();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public void setFileName(string fileName)
|
|
{
|
|
saveFilePath = Path.Combine(Application.persistentDataPath, fileName);
|
|
}
|
|
|
|
public void setAbsoluteFilePath(string path)
|
|
{
|
|
saveFilePath = path;
|
|
}
|
|
|
|
public void saveCommands()
|
|
{
|
|
CommandSaveUtil.saveLinkedList(commandList, saveFilePath);
|
|
}
|
|
|
|
public void saveCommandsToPath(string customPath)
|
|
{
|
|
CommandSaveUtil.saveLinkedList(commandList, customPath);
|
|
}
|
|
|
|
public void loadCommands()
|
|
{
|
|
commandList = CommandSaveUtil.loadLinkedList(saveFilePath);
|
|
}
|
|
|
|
public void loadCommandsFromPath(string customPath)
|
|
{
|
|
commandList = CommandSaveUtil.loadLinkedList(customPath);
|
|
}
|
|
|
|
public IEnumerator excuteAllCommands(Action action)
|
|
{
|
|
if (commandList == null || commandList.Count == 0)
|
|
{
|
|
action?.Invoke();
|
|
yield break;
|
|
}
|
|
yield return null; // Wait for the next frame
|
|
foreach (CommandDTO commandDTO in commandList)
|
|
{
|
|
if (commandDTO == null)
|
|
continue;
|
|
|
|
ICommand command = commandDTO.converToCommand();
|
|
if (command != null)
|
|
{
|
|
command.execute();
|
|
Debug.Log("Executed command from DTO: " + commandDTO.CommandName);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Failed to convert CommandDTO to ICommand.");
|
|
}
|
|
yield return null; // Wait for the next frame
|
|
}
|
|
action.Invoke();
|
|
}
|
|
|
|
public void addLastCommand(CommandDTO command)
|
|
{
|
|
commandList.AddLast(command);
|
|
}
|
|
|
|
public void removeLastCommand()
|
|
{
|
|
if (commandList.Count > 0)
|
|
{
|
|
commandList.RemoveLast();
|
|
}
|
|
}
|
|
|
|
public void clearCommandList()
|
|
{
|
|
commandList.Clear();
|
|
}
|
|
}
|