Resource Manager Plugin – Arma Reforger
Jump to navigation
Jump to search
No edit summary |
Lou Montana (talk | contribs) m (Text replacement - "Script Template plugin" to "{{Link|Arma Reforger:Script Editor: Fill From Template Plugin|Script Template plugin}}") |
||
Line 10: | Line 10: | ||
* In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl|[[OFPEC_tags|TAG_]]TutorialPlugin.c}} (must end with {{hl|Plugin}} by convention) | * In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl|[[OFPEC_tags|TAG_]]TutorialPlugin.c}} (must end with {{hl|Plugin}} by convention) | ||
* Double-click the file to open it | * Double-click the file to open it | ||
* Press {{Controls|Ctrl|T}} to use the | * Press {{Controls|Ctrl|T}} to use the {{Link|Arma Reforger:Script Editor: Fill From Template Plugin|Script Template plugin}} | ||
** In its window, select "Class Type: WorkbenchPlugin", '''set the parent class to {{Link/Enfusion|armaR|ResourceManagerPlugin}}''' and leave the other fields blank/default | ** In its window, select "Class Type: WorkbenchPlugin", '''set the parent class to {{Link/Enfusion|armaR|ResourceManagerPlugin}}''' and leave the other fields blank/default | ||
** A Workbench plugin skeleton is inserted.<!-- | ** A Workbench plugin skeleton is inserted.<!-- |
Revision as of 15:50, 7 May 2024
This tutorial teaches how to create a Resource Manager-specific plugin.
Setup
- Open Script Editor
- In an addon, create a new script in WorkbenchGame
/ResourceManager - name it TAG_TutorialPlugin.c (must end with Plugin by convention) - Double-click the file to open it
- Press Ctrl + T to use the Script Template plugin
- In its window, select "Class Type: WorkbenchPlugin", set the parent class to ResourceManagerPlugin and leave the other fields blank/default
- A Workbench plugin skeleton is inserted.
- In the Run() method, write Print("It works!"); and save the file
- Reload Workbench scripts via Reload WB Scripts option located in Plugins→Settings menu (default shortcut: Ctrl + ⇧ Shift + R)
- The TAG_TutorialPlugin plugin should appear in the Resource Manager's Plugins list, available in the top bar - click on the plugin entry
- "It works!" gets printed in the output console.
Contextual Menu Option
The Resource Browser's Contextual Menu can provide a Plugin option allowing to run code on the selected resource(s) of the defined type(s).
In the WorkbenchPluginAttribute, the resourceTypes parameter must be filled, e.g resourceTypes: { "et", "c" }.
The OnResourceContextMenu method must be overridden to work with said resources.
[WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, resourceTypes: { "et", "c" })]
class TAG_TutorialPlugin : ResourceManagerPlugin
{
//------------------------------------------------------------------------------------------------
override void OnResourceContextMenu(notnull array<ResourceName> resources)
{
Print("Resource context menu action has been called! Here are the selected resources:");
foreach (ResourceName resource : resources)
{
if (resource.EndsWith("c"))
Print("- Script File: " + resource);
else
Print("- Prefab File: " + resource);
}
}
}
ResourceManager Module API
The Resource Manager API allows to register resources, rebuild them, get the selected ones or get their meta file content. In the below code:
- the GetResourceBrowserSelection method is used to temporarily store Resource Browser-selected resources in m_aResourceNames and their file path in m_aFilePaths
- the GetMetaFile method is used to get the current resource's .meta file as a MetaFile instance.
[WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, awesomeFontCode: 0xF188)]
class TAG_TutorialPlugin : ResourceManagerPlugin
{
protected ref array<ResourceName> m_aResourceNames;
protected ref array<string> m_aFilePaths;
//------------------------------------------------------------------------------------------------
override void Run()
{
ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
m_aResourceNames = {};
m_aFilePaths = {};
resourceManager.GetResourceBrowserSelection(WorkbenchSearchResourcesCallbackMethod, false); // non-recursive search
if (m_aFilePaths.IsEmpty())
{
Print("No Resources selected in Resource Browser");
return;
}
MetaFile metaFile;
foreach (int i, ResourceName filePath : m_aFilePaths)
{
metaFile = resourceManager.GetMetaFile(filePath);
if (!metaFile) // e.g directory
continue;
PrintFormat(
"#%1: %3 (dir %2)",
i,
metaFile.GetSourceFilePath(), // returns the directory tree, e.g $MyMod:Path/To/
metaFile.GetResourceID()); // returns the file's ResourceName, equal to 'resName' below
}
}
//------------------------------------------------------------------------------------------------
// \param[in] resName
// \param[in] filePath is in format $MyMod:Path/To/File.ext
protected void WorkbenchSearchResourcesCallbackMethod(ResourceName resName, string filePath = "")
{
m_aResourceNames.Insert(resName);
m_aFilePaths.Insert(filePath);
}
}