Skip to content

[MAJOR] Simplify the design #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Config/EditorEvents.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ attribute "resource";
attribute "strict_type";

table SettingsUpdateFromEditor{
module_name: string;
plugin_name: string;
entry: nos.sys.settings.SettingsEntry;
}

Expand All @@ -26,6 +26,6 @@ table SettingsEditorItem{

// List of items to be displayed in the editor
table SettingsUpdateFromSubsystem{
module_name: string;
plugin_name: string;
settings: [SettingsEditorItem];
}
19 changes: 4 additions & 15 deletions Config/Types.fbs
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
namespace nos.sys.settings;

attribute "hidden_property";
attribute "units";
attribute "builtin";
attribute "skip_make";
attribute "skip_break";
attribute "skip_delay";
attribute "dynamic";
attribute "resource";
attribute "strict_type";
attribute "key";

table SettingsEntry{
entry_name: string (key);
type_name: string;
target_name: string;
data: [ubyte] (dynamic: "type_name");
}

table ModuleSettings{
module_name:string;
module_version:string;
// This is the root table for the global settings file.
// If you change this or any subtable, you should bump minor version
table SettingsList{
settings: [SettingsEntry];
}

table ModuleSettingsList{
module_settings: [ModuleSettings];
}
101 changes: 73 additions & 28 deletions Include/nosSettingsSubsystem/nosSettingsSubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,56 @@
#define NOS_SETTINGS_SUBSYSTEM_H_INCLUDED
#include "Nodos/Types.h"

typedef enum nosSettingsFileDirectory {
NOS_SETTINGS_FILE_DIRECTORY_LOCAL, // Module's root folder
NOS_SETTINGS_FILE_DIRECTORY_WORKSPACE, // Engine's config folder
NOS_SETTINGS_FILE_DIRECTORY_GLOBAL // AppData folder
} nosSettingsFileDirectory;

typedef nosResult(*nosPfnSettingsItemUpdate)(const char* entryName, nosBuffer itemValue);
// NOS_RESULT_SUCCESS: Item value will be serialized to the WriteDirectories
// NOS_RESULT_FAIL: Default item value will be serialized to the WriteDirectories
typedef nosResult(*nosPfnSettingsEntryUpdate)(nosName entryName, nosBuffer itemValue);
typedef uint64_t nosSettingsEntryId;

#if NOS_HAS_CPLUSPLUS_20
#include "EditorEvents_generated.h"
typedef nos::sys::settings::editor::SettingsEditorItem nosSettingsEditorItem;
typedef nos::fb::Visualizer* nosSettingsEditorVisualizer;
#else
typedef void* nosSettingsEditorItem;
typedef void* nosSettingsEditorVisualizer;
#endif

typedef enum nosSettingsFileDirectory {
NOS_SETTINGS_FILE_DIRECTORY_LOCAL = 1 << 0, // Module's root folder
NOS_SETTINGS_FILE_DIRECTORY_WORKSPACE = 1 << 1, // Engine's config folder
NOS_SETTINGS_FILE_DIRECTORY_GLOBAL = 1 << 2 // AppData folder
} nosSettingsFileDirectory;

typedef uint32_t nosSettingsFileDirectoryFlag;

typedef struct nosSettingsEntryParams {
nosName TypeName;
nosBuffer Buffer; // Don't forget to free this buffer
const char* EntryName; // If NULL, "default" will be used
nosSettingsFileDirectory Directory;
// Default value of the buffer
// If nosPfnSettingsEntryUpdate callback returns NOS_SETTINGS_ENTRY_NON_COMPATIBLE, this will be saved
// Don't forget to free this buffer
const nosBuffer* Buffer;
nosName EntryName; // If NULL, "default" will be used
nosSettingsFileDirectoryFlag WriteDirectories; // Directory flags to determine where this entry will be stored
nosPfnSettingsEntryUpdate UpdateCallback; // Callback to update the entry value
bool IsEditableFromEditor; // If true, this entry will be editable from editor
nosSettingsEditorVisualizer Visualizer; // Visualizer for the entry in editor
nosName DisplayName;
// Target module name, editor can decide where to place this entry
// For example, if this is "nos.sys.device", editor can place this entry under "Devices" section
nosName TargetName;
} nosSettingsEntryParams;

typedef struct nosSettingsSubsystem
{
// parameters->directory will be set to found directory
// if parameters->typeName == 0, it will be set to first typename found in the closest file
// parameters->directory is ignored because closest one is chosen
nosResult(NOSAPI_CALL *ReadSettingsEntry)(nosSettingsEntryParams* parameters);
// parameters->typeName should be a valid typename
// if parameters->directory is not set, it will be set to NOS_SETTINGS_FILE_DIRECTORY_LOCAL
nosResult(NOSAPI_CALL *WriteSettingsEntry)(const nosSettingsEntryParams* parameters);


// Editor related functions

nosResult(NOSAPI_CALL *RegisterEditorSettings)(u64 itemCount, const nosSettingsEditorItem** itemList, nosPfnSettingsItemUpdate itemUpdateCallback, nosSettingsFileDirectory saveDirectory);
nosResult(NOSAPI_CALL *UnregisterEditorSettings)();
nosResult(NOSAPI_CALL *RegisterEntry)(nosSettingsEntryParams* parameters);
nosResult(NOSAPI_CALL* UpdateEntryValue)(nosName entryName, nosBuffer value);
// An entry can only be unregistered by the plugin it's registered from
void(NOSAPI_CALL* UnregisterEntry)(nosName entryName);
} nosSettingsSubsystem;

#pragma region Helper Declarations & Macros
// Make sure these are same with nossys file.
#define NOS_SETTINGS_SUBSYSTEM_NAME "nos.sys.settings"

#define NOS_SETTINGS_SUBSYSTEM_VERSION_MAJOR 0
#define NOS_SETTINGS_SUBSYSTEM_VERSION_MINOR 10
#define NOS_SETTINGS_SUBSYSTEM_VERSION_MAJOR 1
#define NOS_SETTINGS_SUBSYSTEM_VERSION_MINOR 0

extern struct nosPluginInfo nosSettingsModuleInfo;
extern nosSettingsSubsystem* nosSettings;
Expand All @@ -61,6 +66,46 @@ extern nosSettingsSubsystem* nosSettings;

#define NOS_SETTINGS_IMPORT() NOS_IMPORT_DEP(NOS_SETTINGS_SUBSYSTEM_NAME, nosSettingsModuleInfo, nosSettings)

#if NOS_HAS_CPLUSPLUS_20
#include "Types_generated.h"
#include <Nodos/Name.hpp>

namespace nos::sys::settings {
inline nosResult RegisterEntry(std::string const& entryName, std::string const& typeName, nosPfnSettingsEntryUpdate updateCallback, std::optional<nosBuffer> defaultVal = std::nullopt, nosSettingsFileDirectoryFlag writeDirectories = NOS_SETTINGS_FILE_DIRECTORY_WORKSPACE, bool editableFromEditor = true, std::optional<std::string> displayName = std::nullopt, std::optional<std::string> targetName = std::nullopt, std::optional<nos::fb::TVisualizer> visualizer = std::nullopt) {
nosSettingsEntryParams params{};
params.EntryName = nos::Name(entryName);
params.TypeName = nos::Name(typeName);
params.UpdateCallback = updateCallback;

if (displayName.has_value())
params.DisplayName = nos::Name(*displayName);
else
params.DisplayName = params.EntryName;

if (defaultVal.has_value())
params.Buffer = &(*defaultVal);

params.WriteDirectories = writeDirectories;
params.IsEditableFromEditor = editableFromEditor;

if (targetName.has_value())
params.TargetName = nos::Name(*targetName);

nos::Buffer visualizerBuf;
if (visualizer.has_value()) {
visualizerBuf = nos::Buffer::From(*visualizer);
params.Visualizer = visualizerBuf.As<nos::fb::Visualizer>();
}

return nosSettings->RegisterEntry(&params);
}

inline void UnregisterEntry(std::string const& entryName) {
nosSettings->UnregisterEntry(nos::Name(entryName));
}
}
#endif // NOS_HAS_CPLUSPLUS_20

#pragma endregion


Expand Down
2 changes: 1 addition & 1 deletion Settings.nossys
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"info": {
"id": {
"name": "nos.sys.settings",
"version": "0.10.3"
"version": "1.0.0"
},
"display_name": "Settings Subsystem",
"description": "Store all module settings",
Expand Down
Loading