-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_guest.cpp
92 lines (74 loc) · 3.8 KB
/
basic_guest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <iostream>
#include "cr.h"
#include <GU/GU_Detail.h>
static GU_Detail *g_data = nullptr;
// To save states automatically from previous instance to a new loaded one, use CR_STATE flag on statics/globals.
// This will create a new data section in the binary for transferable states between instances that will be copied
// and kept around to overwrite the new instance initial states. That means that things that were initialized in
// a previous loaded states will continue from where they were.
static unsigned int CR_STATE version = 1;
// But unfortunately new added states in new instances (post the fact states) do not work because they will be
// overwritten by previous states (in this case, inexisting ones being zeroed), so uncommenting the following line
// will have a value of zero.
//static int32_t CR_STATE sad_state = 2;
// For this reason, cr is better for runtime modifing existing stuff than adding/removing new stuff.
// At the sime time, if we remove a state variable, the reload will safely fail with the error CR_STATE_INVALIDATED.
// A rollback will be effectued and this error can be dealt client side, for exemple, by poping a dialog box asking
// to force reload cleaning up states (restarting the client from scratch with the new version).
void hello(struct cr_plugin *ctx) {
// this demonstrate how to transfer an state between instances by using CR_STATE tag.
// in this case, we track a flag to indicate if hello was print or not, so each new reload
// after the initial one will not print the hello world message.
static bool CR_STATE said_hello = false;
if (!said_hello) {
said_hello = true;
fprintf(stdout, "hello from guest plugin!\n");
}
static int skip = 0;
g_data = (GU_Detail *)ctx->userdata;
std::cout << g_data << std::endl;
if (g_data) {
std::cout << "---Access GU_Detail from guest: " << g_data->getNumPoints() << std::endl;
}
}
void test_crash() {
int *addr = NULL; (void)addr; // warning
// to test crash protection, uncomment the following line
//int i = *addr;
}
CR_EXPORT int cr_main(struct cr_plugin *ctx, enum cr_op operation) {
assert(ctx);
if (operation != CR_STEP) {
fprintf(stdout, "OP: %s(%d)\n", operation == CR_LOAD ? "LOAD" : "UNLOAD", ctx->version);
int *addr = NULL; (void)addr; // warning
// to test crash protection during load
//int i = *addr;
return 0;
}
// crash protection may cause the version to decrement. So we can test current version against one
// tracked between instances with CR_STATE to signal that we're not running the most recent instance.
if (ctx->version < version) {
// a failure code is acessible in the `failure` variable from the `cr_plugin` context.
// on windows this is the structured exception error code, for more info:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms679356(v=vs.85).aspx
fprintf(stdout, "A rollback happened due to failure: %x!\n", ctx->failure);
}
version = ctx->version;
// Not this does not carry state between instances (no CR_STATE), this means each time we load an instance
// this value will be reset to its initial state (true), and then we can print the loaded instance version
// one time only by instance version.
static bool print_version = true;
if (print_version) {
fprintf(stdout, "loaded version: %d\n", ctx->version);
// disable further printing for this instance only
print_version = false;
}
hello(ctx);
test_crash();
//std::this_thread::sleep_for(std::chrono::milliseconds(500));
return 0;
}