Skip to content

Commit fc5d045

Browse files
committed
Make it go.
1 parent b61700b commit fc5d045

File tree

6 files changed

+102
-11
lines changed

6 files changed

+102
-11
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
cmake_minimum_required(VERSION 3.21)
44

55
# Set your project name. This will be the name of your SKSE .dll file.
6-
project(HelloWorld VERSION 0.0.1 LANGUAGES CXX)
6+
project(SkyrimScripting.StartupScript VERSION 0.0.1 LANGUAGES CXX)
77

88
# Set <DEPLOY_ROOT> to set the path of a mod to deploy files to.
99
# Defaults to the value of environment variable SKYRIM_MODS_FOLDER

CMakePresets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"architecture": { "value": "x64", "strategy": "external" },
1111
"cacheVariables": {
1212
"CMAKE_CXX_COMPILER": "cl.exe",
13-
"CMAKE_CXX_FLAGS": "/permissive- /Zc:preprocessor /EHsc /MP /W4 -DWIN32_LEAN_AND_MEAN -DNOMINMAX -DUNICODE -D_UNICODE",
13+
"CMAKE_CXX_FLAGS": "/permissive- /Zc:preprocessor /EHsc /MP /W4 -DWIN32_LEAN_AND_MEAN -DNOMINMAX -DUNICODE -D_UNICODE -D_CRT_SECURE_NO_WARNINGS",
1414
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
1515
"VCPKG_TARGET_TRIPLET": "x64-windows-static-md",
1616
"VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/cmake",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"C:\Program Files\AutoHotkey\AutoHotkey.exe" "%~dp0\main_menu_coc.ahk"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Sleep 2000
2+
WinActivate, ahk_exe SkyrimSE.exe
3+
SetKeyDelay, 0, 50
4+
Sleep 2000
5+
Send {~}
6+
Sleep 1000
7+
Send coc riverwood
8+
Sleep 500
9+
Send {Enter}

Example/coc riverwood/meta.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[General]
2+
modid=0
3+
version=
4+
newestVersion=
5+
category=0
6+
installationFile=
7+
8+
[installedFiles]
9+
size=0

plugin.cpp

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,84 @@
1-
SKSEPluginLoad(const SKSE::LoadInterface *skse) {
2-
SKSE::Init(skse);
1+
#include <Windows.h>
2+
#include <spdlog/sinks/basic_file_sink.h>
3+
4+
#include <filesystem>
5+
6+
namespace logger = SKSE::log;
7+
8+
constexpr auto MAIN_MENU_NAME = "Main Menu";
9+
constexpr auto START_SCRIPT_PATH = "Data/StartupScript.bat";
10+
constexpr auto START_SCRIPT_ENV_VAR = "SKYRIM_STARTUP_SCRIPT";
11+
12+
void RunScript(const std::string& scriptPath) {
13+
logger::info("Running script '{}'", scriptPath);
14+
WinExec(scriptPath.c_str(), SW_HIDE);
15+
}
316

4-
// Once all plugins and mods are loaded, then the ~ console is ready and can
5-
// be printed to
6-
SKSE::GetMessagingInterface()->RegisterListener([](SKSE::MessagingInterface::Message *message) {
7-
if (message->type == SKSE::MessagingInterface::kDataLoaded)
8-
RE::ConsoleLog::GetSingleton()->Print("Hello, world!");
9-
});
17+
void RunStartupScript() {
18+
if (std::filesystem::exists(START_SCRIPT_PATH)) {
19+
logger::info("Found file '{}'", START_SCRIPT_PATH);
20+
RunScript(START_SCRIPT_PATH);
21+
} else {
22+
auto* scriptPathFromENV = std::getenv(START_SCRIPT_ENV_VAR);
23+
if (scriptPathFromENV) {
24+
logger::info("Found environment variable file path '{}'", scriptPathFromENV);
25+
if (std::filesystem::exists(scriptPathFromENV)) {
26+
logger::info("Found file '{}'", scriptPathFromENV);
27+
RunScript(scriptPathFromENV);
28+
}
29+
} else {
30+
logger::info("No startup script provided");
31+
logger::info("");
32+
logger::info("To specify, please add a script named StartupScript.bat to your Skyrim Data folder");
33+
logger::info("or set the SKYRIM_STARTUP_SCRIPT environment variable to the path of a script to run.");
34+
logger::info("");
35+
logger::info(
36+
"Note: after you set the SKYRIM_START_SCRIPT variable, you may need to restart your mod organizer "
37+
"before it will be recognized.");
38+
logger::info(
39+
"Also: Data/StartupScript.bat is checked *before* SKYRIM_START_SCRIPT and will run instead if found.");
40+
}
41+
}
42+
}
1043

44+
void SetupLog() {
45+
auto logsFolder = SKSE::log::log_directory();
46+
if (!logsFolder) {
47+
SKSE::stl::report_and_fail("SKSE log_directory not provided, logs disabled.");
48+
return;
49+
}
50+
auto pluginName = SKSE::PluginDeclaration::GetSingleton()->GetName();
51+
auto logFilePath = *logsFolder / std::format("{}.log", pluginName);
52+
auto fileLoggerPtr = std::make_shared<spdlog::sinks::basic_file_sink_mt>(logFilePath.string(), true);
53+
auto loggerPtr = std::make_shared<spdlog::logger>("log", std::move(fileLoggerPtr));
54+
spdlog::set_default_logger(std::move(loggerPtr));
55+
spdlog::set_level(spdlog::level::trace);
56+
spdlog::flush_on(spdlog::level::info);
57+
}
58+
59+
class EventProcessor : public RE::BSTEventSink<RE::MenuOpenCloseEvent> {
60+
EventProcessor() = default;
61+
~EventProcessor() = default;
62+
EventProcessor(const EventProcessor&) = delete;
63+
EventProcessor(EventProcessor&&) = delete;
64+
EventProcessor& operator=(const EventProcessor&) = delete;
65+
EventProcessor& operator=(EventProcessor&&) = delete;
66+
67+
public:
68+
static EventProcessor& GetSingleton() {
69+
static EventProcessor singleton;
70+
return singleton;
71+
}
72+
RE::BSEventNotifyControl ProcessEvent(const RE::MenuOpenCloseEvent* event,
73+
RE::BSTEventSource<RE::MenuOpenCloseEvent>*) override {
74+
if (event->opening && event->menuName == MAIN_MENU_NAME) RunStartupScript();
75+
return RE::BSEventNotifyControl::kContinue;
76+
}
77+
};
78+
79+
SKSEPluginLoad(const SKSE::LoadInterface* skse) {
80+
SKSE::Init(skse);
81+
SetupLog();
82+
RE::UI::GetSingleton()->AddEventSink<RE::MenuOpenCloseEvent>(&EventProcessor::GetSingleton());
1183
return true;
12-
}
84+
}

0 commit comments

Comments
 (0)