Files
2026-02-21 16:45:37 +08:00

134 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Crosstales.Radio.Model.Entry;
using Crosstales.Radio.Model.Enum;
using Crosstales.Radio.Util;
using UnityEngine;
namespace Crosstales.Radio.Provider
{
[HelpURLAttribute("https://www.crosstales.com/media/data/assets/radio/api/class_crosstales_1_1_radio_1_1_provider_1_1_radio_provider_user.html")]
public class RadioProviderUser : BaseRadioProvider
{
[Header("Save Behaviour")]
[Tooltip("Calls 'Save' OnDisable (default: true).")]
public bool SaveOnDisable = true;
[Header("Source Settings")]
[Tooltip("User radio station entry.")]
public RadioEntryUser Entry;
public override List<BaseRadioEntry> RadioEntries
{
get
{
List<BaseRadioEntry> list = new List<BaseRadioEntry>();
list.Add(Entry);
return list;
}
}
public void OnDisable()
{
if (!Helper.isEditorMode && SaveOnDisable)
{
Save(Entry.FinalPath);
}
}
public override void OnValidate()
{
if (Entry != null && !Entry.isInitalized)
{
Entry.LoadOnlyOnce = true;
}
base.OnValidate();
}
public void Delete()
{
if (File.Exists(Entry.FinalPath))
{
try
{
File.Delete(Entry.FinalPath);
}
catch (IOException ex)
{
UnityEngine.Debug.LogError("Could not delete file: " + Entry.FinalPath + Environment.NewLine + ex);
}
}
}
public void ShowFile()
{
if (Helper.isStandalonePlatform)
{
string directoryName = Path.GetDirectoryName(Entry.FinalPath);
directoryName = ((!Helper.isWindowsPlatform) ? (directoryName + "/") : (directoryName + "\\"));
Process.Start(directoryName);
}
else
{
UnityEngine.Debug.LogWarning("'ShowFile' is not supported on your platform!");
}
}
public void EditFile()
{
if (Helper.isStandalonePlatform)
{
string finalPath = Entry.FinalPath;
if (!File.Exists(finalPath))
{
return;
}
if (Helper.isMacOSPlatform)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "open";
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(Entry.FinalPath) + "/";
process.StartInfo.Arguments = "-t " + Path.GetFileName(finalPath);
process.Start();
return;
}
}
Process.Start(finalPath);
}
else
{
UnityEngine.Debug.LogWarning("'EditFile' is not supported on your platform!");
}
}
protected override void init()
{
base.init();
if (Entry == null || !Entry.EnableSource)
{
return;
}
if (!string.IsNullOrEmpty(Entry.FinalPath) && File.Exists(Entry.FinalPath))
{
StartCoroutine(loadWeb(addCoRoutine(), new RadioEntryURL(Entry, Constants.PREFIX_FILE + Entry.FinalPath, DataFormatURL.Text), true));
}
if (Entry.Resource != null && (!Entry.LoadOnlyOnce || (Entry.LoadOnlyOnce && !File.Exists(Entry.FinalPath))))
{
StartCoroutine(loadResource(addCoRoutine(), new RadioEntryResource(Entry, Entry.Resource, Entry.DataFormat, Entry.ReadNumberOfStations), true));
if (!File.Exists(Entry.FinalPath))
{
Invoke("save", 2f);
}
}
}
private void save()
{
Save(Entry.FinalPath);
}
}
}