Implement a resource system.

Currently, icons, fonts, etc are converted to C structures at compile
time and are hardcoded to the binary. This presents several problems:

  * Cross-compilation is complicated. Right now, it is necessary
    to be able to run executables for the target platform; this
    happens to work with wine-binfmt installed, but is rather ugly.

  * Icons can only have one resolution. On OS X, modern software is
    expected to take advantage of high-DPI ("Retina") screens and
    use so-called @2x assets when ran in high-DPI mode.

  * Localization is complicated. Win32 and OS X provide built-in
    support for loading the resource appropriate for the user's
    locale.

  * Embedding strings can only be done as raw strings, using C++'s
    R"(...)" literals. This precludes embedding sizable strings,
    e.g. JavaScript libraries as used in Three.js export, and makes
    git history less useful. Not embedding the libraries means we
    have to rely on external CDNs, which requires an Internet
    connection and adds a glaring point of failure.

  * Linux distribution guidelines are violated. All architecture-
    independent data, especially large data such as fonts, is
    expected to be in /usr/share, not in the binary.

  * Customization is impossible without recompilation. Minor
    modifications like adding a few missing vector font characters
    or adjusting localization require a complete development
    environment, which is unreasonable to expect from users of
    a mechanical CAD.

As such, this commit adds a resource system that bundles (and
sometimes builds) resources with the executable. Where they go is
platform-dependent:

  * on Win32: into resources of the executable, which allows us to
    keep distributing one file;
  * on OS X: into the app bundle;
  * on other *nix: into /usr/share/solvespace/ or ../res/ (relative
    to the executable path), the latter allowing us to run freshly
    built executables without installation.

It also subsides the platform-specific resources that are in src/.

The resource system is not yet used for anything; this will be added
in later commits.
pull/10/head
whitequark 2016-04-21 15:54:18 +00:00
parent b2cdbe8c8d
commit f4c01f670c
12 changed files with 224 additions and 1 deletions

View File

@ -184,5 +184,6 @@ endif()
# components # components
add_subdirectory(tools) add_subdirectory(tools)
add_subdirectory(res)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(exposed) add_subdirectory(exposed)

107
res/CMakeLists.txt Normal file
View File

@ -0,0 +1,107 @@
# First, set up registration functions for the kinds of resources we handle.
set(resource_root ${CMAKE_CURRENT_SOURCE_DIR}/)
set(resource_list)
if(WIN32)
set(rc_file ${CMAKE_CURRENT_BINARY_DIR}/resources.rc)
file(WRITE ${rc_file} "// Autogenerated; do not edit\n")
function(add_resource name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
list(GET "${ARGN}" 0 id)
if(id STREQUAL NOTFOUND)
string(REPLACE ${resource_root} "" id ${source})
endif()
list(GET "${ARGN}" 1 type)
if(type STREQUAL NOTFOUND)
set(type RCDATA)
endif()
file(SHA512 "${source}" hash)
file(APPEND ${rc_file} "${id} ${type} \"${source}\" // ${hash}\n")
# CMake doesn't track file dependencies across directories, so we force
# a reconfigure (which changes the RC file because of the hash above)
# every time a resource is changed.
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${source}")
endfunction()
elseif(APPLE)
set(app_resource_dir ${CMAKE_BINARY_DIR}/src/solvespace.app/Contents/Resources)
function(add_resource name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
set(target ${app_resource_dir}/${name})
set(resource_list "${resource_list};${target}" PARENT_SCOPE)
get_filename_component(target_dir ${target} DIRECTORY)
add_custom_command(
OUTPUT ${target}
COMMAND ${CMAKE_COMMAND} -E make_directory ${target_dir}
COMMAND ${CMAKE_COMMAND} -E copy ${source} ${target}
COMMENT "Copying resource ${name}"
DEPENDS ${source}
VERBATIM)
endfunction()
function(add_xib name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
get_filename_component(basename ${name} NAME_WE)
set(target ${app_resource_dir}/${basename}.nib)
set(resource_list "${resource_list};${target}" PARENT_SCOPE)
add_custom_command(
OUTPUT ${target}
COMMAND ${CMAKE_COMMAND} -E make_directory ${app_resource_dir}
COMMAND ibtool --errors --warnings --notices --output-format human-readable-text
--compile ${target} ${source}
COMMENT "Building Interface Builder file ${name}"
DEPENDS ${source}
VERBATIM)
endfunction()
function(add_iconset name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
get_filename_component(basename ${name} NAME_WE)
set(target ${app_resource_dir}/${basename}.icns)
set(resource_list "${resource_list};${target}" PARENT_SCOPE)
add_custom_command(
OUTPUT ${target}
COMMAND ${CMAKE_COMMAND} -E make_directory ${app_resource_dir}
COMMAND iconutil -c icns -o ${target} ${source}
COMMENT "Building icon set ${name}"
DEPENDS ${source}
VERBATIM)
endfunction()
else() # Unix
include(GNUInstallDirs)
set(app_resource_dir ${CMAKE_BINARY_DIR}/res)
function(add_resource name)
set(source ${CMAKE_CURRENT_SOURCE_DIR}/${name})
set(target ${app_resource_dir}/${name})
set(resource_list "${resource_list};${target}" PARENT_SCOPE)
get_filename_component(target_dir ${target} DIRECTORY)
add_custom_command(
OUTPUT ${target}
COMMAND ${CMAKE_COMMAND} -E make_directory ${target_dir}
COMMAND ${CMAKE_COMMAND} -E copy ${source} ${target}
COMMENT "Copying resource ${name}"
DEPENDS ${source}
VERBATIM)
get_filename_component(name_dir ${name} DIRECTORY)
install(FILES ${source}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/solvespace/${name_dir})
endfunction()
endif()
# Second, register all resources.
add_resource(banner.txt)
# Third, distribute the resources.
add_custom_target(resources
DEPENDS ${resource_list})
if(WIN32)
set_property(TARGET resources PROPERTY EXTRA_SOURCES ${rc_file})
endif()

1
res/banner.txt Normal file
View File

@ -0,0 +1 @@
SolveSpace!

View File

@ -289,6 +289,7 @@ set(solvespace_SOURCES
modify.cpp modify.cpp
mouse.cpp mouse.cpp
polygon.cpp polygon.cpp
resource.cpp
request.cpp request.cpp
solvespace.cpp solvespace.cpp
style.cpp style.cpp
@ -318,7 +319,11 @@ add_executable(solvespace WIN32 MACOSX_BUNDLE
${generated_SOURCES} ${generated_SOURCES}
${generated_HEADERS} ${generated_HEADERS}
${solvespace_HEADERS} ${solvespace_HEADERS}
${solvespace_SOURCES}) ${solvespace_SOURCES}
$<TARGET_PROPERTY:resources,EXTRA_SOURCES>)
add_dependencies(solvespace
resources)
target_link_libraries(solvespace target_link_libraries(solvespace
dxfrw dxfrw

View File

@ -1130,6 +1130,26 @@ std::vector<std::string> SolveSpace::GetFontFiles() {
return fonts; return fonts;
} }
const void *SolveSpace::LoadResource(const std::string &name, size_t *size) {
static NSMutableDictionary *cache;
if(cache == nil) {
cache = [[NSMutableDictionary alloc] init];
}
NSString *key = [NSString stringWithUTF8String:name.c_str()];
NSData *data = [cache objectForKey:key];
if(data == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:key ofType:nil];
data = [[NSFileHandle fileHandleForReadingAtPath:path] readDataToEndOfFile];
if(data == nil) oops();
[cache setObject:data forKey:key];
}
*size = [data length];
return [data bytes];
}
/* Application lifecycle */ /* Application lifecycle */
@interface ApplicationDelegate : NSObject<NSApplicationDelegate> @interface ApplicationDelegate : NSObject<NSApplicationDelegate>

View File

@ -3,6 +3,9 @@
#define PACKAGE_VERSION "@solvespace_VERSION_MAJOR@.@solvespace_VERSION_MINOR@~@solvespace_GIT_HASH@" #define PACKAGE_VERSION "@solvespace_VERSION_MAJOR@.@solvespace_VERSION_MINOR@~@solvespace_GIT_HASH@"
/* Non-OS X *nix only */
#define UNIX_DATADIR "@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_DATAROOTDIR@/solvespace"
/* Do we have the si library on win32, or libspnav on *nix? */ /* Do we have the si library on win32, or libspnav on *nix? */
#cmakedefine HAVE_SPACEWARE #cmakedefine HAVE_SPACEWARE

View File

@ -1474,6 +1474,36 @@ std::vector<std::string> GetFontFiles() {
return fonts; return fonts;
} }
static std::string resource_dir;
const void *LoadResource(const std::string &name, size_t *size) {
static std::map<std::string, std::vector<uint8_t>> cache;
auto it = cache.find(name);
if(it == cache.end()) {
struct stat st;
std::string path;
path = (UNIX_DATADIR "/") + name;
if(stat(path.c_str(), &st)) {
if(errno != ENOENT) oops();
path = resource_dir + "/" + name;
if(stat(path.c_str(), &st)) oops();
}
std::vector<uint8_t> data(st.st_size);
FILE *f = ssfopen(path.c_str(), "rb");
if(!f) oops();
fread(&data[0], 1, st.st_size, f);
fclose(f);
cache.emplace(name, std::move(data));
it = cache.find(name);
}
*size = (*it).second.size();
return &(*it).second[0];
}
/* Space Navigator support */ /* Space Navigator support */
#ifdef HAVE_SPACEWARE #ifdef HAVE_SPACEWARE
@ -1536,6 +1566,11 @@ int main(int argc, char** argv) {
ambiguous. */ ambiguous. */
gtk_disable_setlocale(); gtk_disable_setlocale();
resource_dir = argv[0]; // .../src/solvespace
resource_dir.erase(resource_dir.rfind('/'));
resource_dir.erase(resource_dir.rfind('/'));
resource_dir += "/res"; // .../res
Gtk::Main main(argc, argv); Gtk::Main main(argc, argv);
#ifdef HAVE_SPACEWARE #ifdef HAVE_SPACEWARE

18
src/resource.cpp Normal file
View File

@ -0,0 +1,18 @@
//-----------------------------------------------------------------------------
// Discovery and loading of our resources (icons, fonts, templates, etc).
//
// Copyright 2016 whitequark
//-----------------------------------------------------------------------------
#include "solvespace.h"
namespace SolveSpace {
std::string LoadString(const std::string &name) {
size_t size;
const void *data = LoadResource(name, &size);
if(data == NULL) oops();
return std::string(static_cast<const char *>(data), size);
}
}

18
src/resource.h Normal file
View File

@ -0,0 +1,18 @@
//-----------------------------------------------------------------------------
// Discovery and loading of our resources (icons, fonts, templates, etc).
//
// Copyright 2016 whitequark
//-----------------------------------------------------------------------------
#ifndef __RESOURCE_H
#define __RESOURCE_H
// Only the following function is platform-specific.
// It returns a pointer to resource contents that is aligned to at least
// sizeof(void*) and has a global lifetime, or NULL if a resource with
// the specified name does not exist.
const void *LoadResource(const std::string &name, size_t *size);
std::string LoadString(const std::string &name);
#endif

View File

@ -13,6 +13,9 @@ Sketch SolveSpace::SK = {};
std::string SolveSpace::RecentFile[MAX_RECENT] = {}; std::string SolveSpace::RecentFile[MAX_RECENT] = {};
void SolveSpaceUI::Init() { void SolveSpaceUI::Init() {
// Check that the resource system works.
dbp("%s", LoadString("banner.txt").data());
SS.tangentArcRadius = 10.0; SS.tangentArcRadius = 10.0;
// Then, load the registry settings. // Then, load the registry settings.

View File

@ -275,6 +275,8 @@ void MemFree(void *p);
void InitHeaps(void); void InitHeaps(void);
void vl(void); // debug function to validate heaps void vl(void); // debug function to validate heaps
#include "resource.h"
// End of platform-specific functions // End of platform-specific functions
//================ //================

View File

@ -1323,6 +1323,16 @@ static void CreateMainWindows(void)
ClientIsSmallerBy = (r.bottom - r.top) - (rc.bottom - rc.top); ClientIsSmallerBy = (r.bottom - r.top) - (rc.bottom - rc.top);
} }
const void *SolveSpace::LoadResource(const std::string &name, size_t *size) {
HRSRC hres = FindResourceW(NULL, Widen(name).c_str(), RT_RCDATA);
if(!hres) oops();
HGLOBAL res = LoadResource(NULL, hres);
if(!res) oops();
*size = SizeofResource(NULL, hres);
return LockResource(res);
}
#ifdef HAVE_SPACEWARE #ifdef HAVE_SPACEWARE
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Test if a message comes from the SpaceNavigator device. If yes, dispatch // Test if a message comes from the SpaceNavigator device. If yes, dispatch