Resource Manager Plugin – Arma Reforger
Category: Arma Reforger/Modding/Scripting/Workbench/Tutorials
| Lou Montana (talk | contribs)  (Page creation) | Lou Montana (talk | contribs)  m (Text replacement - "\{\{GameCategory\|armaR\|Modding\|(Guidelines|Tutorials)\|([^=↵]*)\}\}" to "{{GameCategory|armaR|Modding|$2|$1}}") | ||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 7: | Line 7: | ||
| == Setup == | == Setup == | ||
| * Open Script Editor | * Open [[Arma_Reforger:Script_Editor|Script Editor]] | ||
| * In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl| | * In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl|[[Scripting 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 Script Template plugin | * 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", 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.<!-- | ||
| * In the {{hl|WorkbenchPluginAttribute}}, replace <enforce inline>wbModules: { "ResourceManager" }</enforce> by <enforce inline>wbModules: { "ScriptEditor" }</enforce>--> | * In the {{hl|WorkbenchPluginAttribute}}, replace <enforce inline>wbModules: { "ResourceManager" }</enforce> by <enforce inline>wbModules: { "ScriptEditor" }</enforce>--> | ||
| * In the <enforce inline>Run()</enforce> method, write <enforce inline>Print("It works!");</enforce> and save the file | * In the <enforce inline>Run()</enforce> method, write <enforce inline>Print("It works!");</enforce> and save the file | ||
| *  | * Reload Workbench scripts via '''Reload WB Scripts''' option located in ''Plugins→Settings'' menu (default shortcut: {{Controls|Ctrl|Shift|R}}) | ||
| * The {{hl| | * The {{hl|[[Scripting Tags|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. | * "It works!" gets printed in the output console. | ||
| Line 33: | Line 33: | ||
| <enforce> | <enforce> | ||
| [WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, resourceTypes: { "et", "c" })] | [WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, resourceTypes: { "et", "c" })] | ||
| class TAG_TutorialPlugin :  | class TAG_TutorialPlugin : ResourceManagerPlugin | ||
| { | { | ||
| 	//------------------------------------------------------------------------------------------------ | 	//------------------------------------------------------------------------------------------------ | ||
| Line 62: | Line 62: | ||
| <enforce methods="WorkbenchSearchResourcesCallbackMethod"> | <enforce methods="WorkbenchSearchResourcesCallbackMethod"> | ||
| [WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, awesomeFontCode: 0xF188)] | [WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, awesomeFontCode: 0xF188)] | ||
| class TAG_TutorialPlugin :  | class TAG_TutorialPlugin : ResourceManagerPlugin | ||
| { | { | ||
| 	protected ref array<ResourceName> m_aResourceNames; | 	protected ref array<ResourceName> m_aResourceNames; | ||
| Line 109: | Line 109: | ||
| {{GameCategory|armaR|Modding | {{GameCategory|armaR|Modding|Scripting|Workbench|Tutorials}} | ||
Latest revision as of 13:31, 26 February 2025
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);
	}
}
