#1 2016-09-25 00:07:26

RaXaR
Member

Embed Mono as scripting language?

Hi folks :)

I'm having a really hard time to embed Mono as the scripting language for Tesseract.
I've been referencing these articles and documents (and a few more from stack overflow etc.):
Embedding mono in a c++ application - Giorgos' Blog
Embedding Mono
Mono Documents - Embedding Mono
So far I've had no luck in getting it to work. Has anyone got some tips or ideas that might help?
Any assistance would be greatly appreciated.

Offline

#2 2016-09-27 04:27:36

chasester
Member

Re: Embed Mono as scripting language?

Seems straigh forward you will need to give me a better idea on what errors you are getting?

Basically download it. Set it up inside scr/includes/mono-2.0.

Then build the lib file using there project folders.

Then link the lib file to sauers directory (in vs that properties->linker->libraries).

Then drag the mono dll in the %root%/bin or from the project files ../../bin (basically where the exe builds too).

Then run make sure there is no errors.

Then follow the c++ tutorial and use the documentation to integrate with cube script.

Offline

#3 2016-09-28 07:59:27

RaXaR
Member

Re: Embed Mono as scripting language?

It looks like the error I'm getting relates to precompiled headers. I checked a few articles and they mention adding #include "stdafx.h". I added it to the top of main.cpp but still no luck. The first warning and error I get is:
warning C4627: '#include <string>': skipped when looking for precompiled header use   
error C3861: 'mono_string_to_utf8': identifier not found
the rest of the errors are similar.

Offline

#4 2016-09-29 03:47:11

chasester
Member

Re: Embed Mono as scripting language?

cube uses precompiled headers, which means that the headers are compiled then main.cpp so forth so on so you need to do all your "global" includes in cube.h. I Recommend creating a mono.h where you include all the needed things. Then just add mono.h in the cube.h header list. By moving #include <string> to cube.h will solve the warning. The second one is because it doesnt know what a string is (cuz cube uses char * not strings) I suggest going into the typedef string and renaming it something like cubestr, this should be in command.h (idb) Then declaring a typedef string std::string. this should solve most of those kinda issues. All strings in cube are just char [MAXSTRING] not true strings :) its safer but now a days its not necessary to do this any more.

I had to do this for my angel script mod. It shouldnt take long to fix.

Offline

#5 2016-09-29 05:52:23

RaXaR
Member

Re: Embed Mono as scripting language?

Wow, thanks for the detailed reply and suggestions! It's really appreciated. The other day I decided to bench integrating mono for a few days because my monitor started looking like a punching bag. Also because I'm clearly not good enough with C/C++ for such a task yet.

However, I will circle back to this - it has to be achieved. When I do then I'll update this post with progress/questions.

Thanks again for the assist. :)

Offline

#6 2016-09-30 20:33:52

catalinux
Member

Re: Embed Mono as scripting language?

I would vote against heavy-weight 3rdParty accessories like bullet, mono, java, angelscript.

Terrseract / Sauer are perfect as they are right now with cubescript.

However, for speed, especially on smartphones I replaced the UI cubescript code with macros called through ICOMMAND(). This way the C++ code is optimized by the compiler and the speed and battery saving is at least 10 times more efficient.

Here are the macros:

#define uispace(spacew, spaceh, children) \
    BUILDI(Spacer, o, o->setup(spacew, spaceh), {children;})

#define uisliderbutton(children) \
    BUILDI(SliderButton, o, o->setup(), { children })

#define uihslider(name, minn, maxx, vstep, onchange, children) \
{ \
    static uint *onchange_##__LINE__ = compilecode(onchange); BUILDI(HorizontalSlider, o, o->setup(readident(#name), minn, maxx, vstep, onchange_##__LINE__), { children } ); \
}

#define UIhslider(name, minn, maxx, step, x, y, onchange, children) \
    uihlist(0, \
        uisliderarrow(-1, uiarrowbutton(y, y, 90, "", .5)); \
        uihslider(name, minn, maxx, step, onchange, \
            uifill(x - 2.0f*y, y, { uicolor(0, 0, c_slider, ); uiclamp_(1,1,1,1); }); \
            uisliderbutton( \
                uicolor(0, y, c_menu,); \
                style_button_hold1 \
                uiclamp__(1,1,1,1); \
                uispace(UIbutton_padding, 0, children); \
            ); \
        ); \
        uisliderarrow(1, uiarrowbutton(y, y, 270, "", .5)); \
    )

#define uicheckbox_widget(condition, size) \
    uicolor(size, size, condition ? c_on1 : c_off, \
        uioutline(c_line,); \
        uiclamp__(1, 1, 1, 1); \
    ) \

#define _uibutton(style, width, height, on_release, alignment, use_padding, children) \
    uitarget(width, height, \
        style; \
        \
        uirelease(on_release); \
        uiclamp__(1, 1, 1, 1); \
        uihlist(0.01, \
            uialign(alignment, 0); \
            children; \
        ); \
    )

#define uibutton(width, height, on_release, alignment, use_padding, children) \
    _uibutton(style_button_default, width, height, on_release, alignment, use_padding, children)

#define uicheckbox(name, description, w, h) \
    _uibutton(, w, h, #name " (! $" #name ")", -1, 0, \
        uicheckbox_widget(name, 0.016); \
        uitext(description, UI_opt_text_size); \
    )

#define uirelease(code) {\
    static uint *releaseee##__LINE__ = compilecode(code); uirelease_(releaseee##__LINE__, nullptr); \
}

#define uitriangle(color, w, h, angle, children) \
    BUILDI(Triangle, o, o->setup(Color(color), w, h, angle, Triangle::SOLID), {children})

#define uifield(name, length, onchange, scale, contents) \
{ \
    static uint *onchange_##__LINE__ = compilecode(onchange); BUILDI(Field, o, o->setup(readident(#name), length, onchange_##__LINE__, (scale <= 0 ? 1 : scale) * uitextscale), {contents} ); \
}

#define uioutline(color, contents) \
    BUILDI(Outline, o, o->setup(Color(color), 0, 0), {contents})
#define uigrid(columns, spacew, spaceh, contents) \
    BUILDI(Grid, o, o->setup(columns, spacew, spaceh), {contents})
#define uicolor(w, h, color, contents) \
    BUILDI(FillColor, o, o->setup(FillColor::SOLID, Color(color), w, h), {contents} )
#define uihlist(size, contents) \
    BUILDI(HorizontalList, o, o->setup(size), { contents })
#define uivlist(size, contents) \
    BUILDI(VerticalList, o, o->setup(size), { contents })
#define uitarget(w, h, contents) \
    BUILDI(Target, o, o->setup(w, h), { contents })
#define uiimage(name, w, h, contents) \
    BUILDI(Image, o, o->setup(textureload(name, 3, true, false), w, h), { contents } )
#define uitext(txt, size) \
{ \
    tagval t; \
    t.setstr(newstring(txt)); \
    buildtext(t, size, uitextscale, Color(255, 255, 255, 255), -1, nullptr); \
    t.cleanup(); \
}

#define uiwrapcontext(txt, wrap, size) \
{ \
    tagval t; \
    t.setstr(newstring(txt)); \
    buildtext(t, size, FONTH*uicontextscale, Color(255, 255, 255), wrap, nullptr); \
    t.cleanup(); \
}

#define getss(fmt) \
    static std::stringstream ss; ss.str(""); ss << fmt;

#define uitextss(fmt, size) \
{ \
    getss(fmt); \
    uitext(ss.str().c_str(), size); \
}
#define uiwrapcontextss(fmt, wrap, size) \
{ \
    getss(fmt); \
    uiwrapcontext(ss.str().c_str(), wrap, size); \
}

#define uicolortext(txt, color, size) \
{ \
    tagval t; \
    t.setstr(newstring(txt)); \
    buildtext(t, size, uitextscale, color, -1, nullptr); \
    t.cleanup(); \
}
#define uicolortextss(fmt, color, size) \
{ \
    getss(fmt); \
    uicolortext(ss.str().c_str(), color, size); \
}
#define uicolortextRGBA(txt, r, g, b, a, size) \
    buildtext(tagval().setstr(newstring(txt)), size, uitextscale, Color(r, g, b, a), -1, nullptr)

#define uifill(w, h, contents) \
    BUILDI(Filler, o, o->setup(w, h), {contents})

#define uisliderarrow(dir, children) \
    BUILDI(SliderArrow, o, o->setup(dir), { children })

#define uiarrowbutton(wsize, hsize, orientation, onreleasee, arrowsizePercent) \
{ \
    float arrowsize = std::min(wsize, hsize) * std::min(1.0f, std::max(0.0f, (float)arrowsizePercent)); \
    uitarget(wsize, hsize, \
        uicolor(0, 0, c_menu, ); \
        style_button_default; \
        uirelease(onreleasee); \
        uiclamp__(1,1,1,1); \
        uitriangle(0xFFFFFF, arrowsize, arrowsize, orientation, ); \
    ); \
}

#define uiscroll(clipw, cliph, clamppedBottom, children) \
    BUILDI(Scroller, o, o->setup(clipw, cliph, clamppedBottom), {children})

#define uitableheader(columndata, children) \
    BUILDCOLUMNSI(TableHeader, o, o->setup(), {columndata}, {children})
#define uitablerow(columndata, children) \
    BUILDCOLUMNSI(TableRow, o, o->setup(), {columndata}, {children})

#define uitable(spacew, spaceh, children) \
    BUILDI(Table, o, o->setup(spacew, spaceh), {children})

#define uivscrollbar(children) \
    BUILDI(VerticalScrollBar, o, o->setup(), {children})

#define uihscrollbar(children) \
    BUILDI(HorizontalScrollBar, o, o->setup(), {children})

#define uiscrollbutton(children) \
    BUILDI(ScrollButton, o, o->setup(), {children})

#define UIvscroll(wsize, hsize) \
   uivlist(0, \
      uivscrollbar( \
         uiscrollbutton( \
            uicolor(wsize, 0, c_fill,); \
            style_button_hold5 \
            uiclamp__(1, 1, 1, 1); \
         ); \
      ); uiclamp_(0, 1, 1, 1); \
   )

#define UIhscroll(wsize, hsize) \
   uivlist(0, \
      uihscrollbar( \
         uiscrollbutton( \
            uicolor(0, hsize, c_fill,); \
            style_button_hold5 \
            uiclamp__(1, 1, 1, 1); \
         ); \
      ); uiclamp_(1, 1, 0, 0); \
   )

And a sample usage:

uivlist(0,
	uifill(0, 0.05, );
	uitarget(0, 0,
		uiimage( "path", 0.2, 0.1,
			uicolortext("text", 0xb2cccc, 1.2);
		);
		uirelease("echo pressed");
	);
	
	uibutton(0.4, 0.1, "echo button pressed", 0, 0, uitext("Button", 1));
	
	uialign__(1, 0);
);

UI cubescript code to c++ translation is straightforward. A tool could be also written after testing first the script and check how it looks and then compiling the code.

Last edited by catalinux (2016-09-30 20:43:58)

Offline

#7 2016-10-01 09:02:45

RaZgRiZ
Moderator

Re: Embed Mono as scripting language?

catalinux wrote:

UI cubescript code to c++ translation is straightforward. A tool could be also written after testing first the script and check how it looks and then compiling the code.

That is a fairly interesting approach. I'm not sure how well you can translate cubescript macros back to c++ or if you can even simulate the styling system without re-working it, but very interesting nonetheless! Definitely some outdated commands in there though :P

Offline

#8 2016-10-01 15:36:17

catalinux
Member

Re: Embed Mono as scripting language?

styling?
it's exactly like in cubescript:

#define style_button_default \
{ \
    uicolor(0.01, 0.01, c_boxmod, ); \
    uioutline(c_line, ); \
}

Offline

#9 2016-10-01 21:23:17

RaZgRiZ
Moderator

Re: Embed Mono as scripting language?

catalinux wrote:

styling?
it's exactly like in cubescript:

#define style_button_default \
{ \
    uicolor(0.01, 0.01, c_boxmod, ); \
    uioutline(c_line, ); \
}

Not what I meant. In my code styles are active pieces of code. A little tough to explain. In the button they're passed as a single argument, but that argument is not static. It can be influenced by its own code.

example:

UIbuttonarea "hold1 (uihover+? $c_diag1 n) d n" 0.32 0.08

In this scenario, the style reads the hover state of the next UI child to be created (in this case it's the UIbuttonarea), and adjusts its 1st parameter accordingly. 'doargs' is a tricky ICOMMAND, without it the whole style in the quotes would be read as-is and wouldn't work.
In simpler words, if you're hovering over UIbuttonarea's box, it automatically changes the style's 1st param to be $c_diag1 instead of "n".

That's what I meant when I said that it might be a little tricky to accurately to emulate this behavior without a redesign. It might not be very efficient in its current state as C++ code.

Last edited by RaZgRiZ (2016-10-01 21:24:31)

Offline

Board footer