Skip to content

Instantly share code, notes, and snippets.

@Tenderfeel
Last active February 17, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tenderfeel/8aea0edc2ff9aa44a22227c0c0b1c5e7 to your computer and use it in GitHub Desktop.
Save Tenderfeel/8aea0edc2ff9aa44a22227c0c0b1c5e7 to your computer and use it in GitHub Desktop.
Advanced Combat Tracker Sample Plugin #p1
using Advanced_Combat_Tracker;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace ACT.SamplePlugin
{
public class SamplePlugin: UserControl1, IActPluginV1
{
private readonly string settingsFile = Path.Combine(ActGlobals.oFormActMain.AppDataFolder.FullName, "Config\\SamplePlugin.config.xml");
SettingsSerializer xmlSettings;
Label lblStatus; // The status label that appears in ACT's Plugin tab
/// <summary>
/// プラグインが有効化されたときに呼び出される
/// </summary>
/// <param name="pluginScreenSpace">プラグインのタブページ</param>
/// <param name="pluginStatusText">プラグインのステータステキスト</param>
public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
{
lblStatus = pluginStatusText; // Hand the status label's reference to our local var
pluginScreenSpace.Text = "SamplePlugin";
pluginScreenSpace.Controls.Add(this);
xmlSettings = new SettingsSerializer(this); // Create a new settings serializer and pass it this instance
LoadSettings();
lblStatus.Text = "Plugin Started";
}
/// <summary>
/// プラグインが無効化されたときに呼び出される
/// </summary>
public void DeInitPlugin()
{
SaveSettings();
lblStatus.Text = "Plugin Exited";
}
/// <summary>
/// プラグイン設定をロードする
/// </summary>
private void LoadSettings()
{
// Add any controls you want to save the state of
xmlSettings.AddControlSetting(textBox1.Name, textBox1);
if (File.Exists(settingsFile))
{
FileStream fs = new FileStream(settingsFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
XmlTextReader xReader = new XmlTextReader(fs);
try
{
while (xReader.Read())
{
if (xReader.NodeType == XmlNodeType.Element)
{
if (xReader.LocalName == "SettingsSerializer")
{
xmlSettings.ImportFromXml(xReader);
}
}
}
}
catch (Exception ex)
{
lblStatus.Text = "Error loading settings: " + ex.Message;
}
xReader.Close();
}
}
/// <summary>
/// プラグイン設定を保存する
/// </summary>
private void SaveSettings()
{
FileStream fs = new FileStream(settingsFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
XmlTextWriter xWriter = new XmlTextWriter(fs, Encoding.UTF8);
xWriter.Formatting = Formatting.Indented;
xWriter.Indentation = 1;
xWriter.IndentChar = '\t';
xWriter.WriteStartDocument(true);
xWriter.WriteStartElement("Config"); // <Config>
xWriter.WriteStartElement("SettingsSerializer"); // <Config><SettingsSerializer>
xmlSettings.ExportToXml(xWriter); // Fill the SettingsSerializer XML
xWriter.WriteEndElement(); // </SettingsSerializer>
xWriter.WriteEndElement(); // </Config>
xWriter.WriteEndDocument(); // Tie up loose ends (shouldn't be any)
xWriter.Flush(); // Flush the file buffer to disk
xWriter.Close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment