Ejemplo n.º 1
0
    private static (SaveInformation info, Save save, Image screenshot) LoadFromFile(string file, bool info,
                                                                                    bool save, bool screenshot, Action readFinished)
    {
        using (var directory = new Directory())
        {
            if (!directory.FileExists(file))
            {
                throw new ArgumentException("save with the given name doesn't exist");
            }
        }

        var(infoStr, saveStr, screenshotData) = LoadDataFromFile(file, info, save, screenshot);

        readFinished?.Invoke();

        SaveInformation infoResult  = null;
        Save            saveResult  = null;
        Image           imageResult = null;

        if (info)
        {
            if (string.IsNullOrEmpty(infoStr))
            {
                throw new IOException("couldn't find info content in save");
            }

            infoResult = ThriveJsonConverter.Instance.DeserializeObject <SaveInformation>(infoStr);
        }

        if (save)
        {
            if (string.IsNullOrEmpty(saveStr))
            {
                throw new IOException("couldn't find save content in save file");
            }

            // This deserializes a huge tree of objects
            saveResult = ThriveJsonConverter.Instance.DeserializeObject <Save>(saveStr);
        }

        if (screenshot)
        {
            imageResult = new Image();

            if (screenshotData != null && screenshotData.Length > 0)
            {
                imageResult.LoadPngFromBuffer(screenshotData);
            }

            // Not a critical error that screenshot is missing even if it was requested
        }

        return(infoResult, saveResult, imageResult);
    }
Ejemplo n.º 2
0
 /// <summary>
 ///   Returns true if file exists
 /// </summary>
 /// <param name="path">Path to check</param>
 /// <returns>True if exists, false otherwise</returns>
 public static bool Exists(string path)
 {
     using var directory = new Directory();
     return(directory.FileExists(path));
 }