Project

General

Profile

SO3Engine
SCOLResources.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25
34#include "SCOLPack/SO3SCOL.h"
35#include "SO3Renderer/SO3Root.h"
39
40
51//TODO REMOVE ?
53{
54#ifdef SO3_DEBUG
55 MMechostr(MSKDEBUG, "SO3AddResourceLocationURL\n");
56#endif
57
58 int p = MMpull(m);
59 int group = MMget(m, 0);
60 if (p == NIL)
61 {
62 MMset(m, 0, NIL);
63 return 0;
64 }
65
66 const char* resourceLocation = MMstartstr(m, MTOP(p));
67
68 std::string groupResource;
69 if (group == NIL)
70 groupResource = Ogre::RGN_DEFAULT;
71 else
72 groupResource = std::string(MMstartstr(m, MTOP(group)));
73
74 try
75 {
76 if (!checkGroupExists(groupResource))
77 {
78 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(resourceLocation, "URL", groupResource);
79 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(groupResource);
80 Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(groupResource);
81 }
82 else
83 {
84 Ogre::FileInfoListPtr finfo;
85 Ogre::FileInfoList::iterator it;
86 finfo = Ogre::ResourceGroupManager::getSingleton().findResourceFileInfo(groupResource, "*");
87 Ogre::ResourceGroupManager::getSingleton().clearResourceGroup(groupResource);
88 for (it = finfo->begin(); it != finfo->end(); ++it)
89 {
90 if (!Ogre::ResourceGroupManager::getSingleton().resourceExists(groupResource, it->basename)) Ogre::ResourceGroupManager::getSingleton().addResourceLocation(it->archive->getName().c_str(), it->archive->getType(), groupResource);
91 }
92 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(resourceLocation, "URL", groupResource);
93 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(groupResource);
94 }
95 MMset(m, 0, ITOM(1));
96 return 0;
97 }
98 catch (Ogre::Exception &e)
99 {
100 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
101 MMset(m, 0, NIL);
102 return 0;
103 }
104}
105
106
118{
119#ifdef SO3_DEBUG
120 MMechostr(MSKDEBUG, "SO3AddResourceLocationZIP\n");
121#endif
122
123 bool exists = false;
124
125 int p = MMpull(m);
126 int group = MMget(m, 0);
127 if (p == NIL)
128 {
129 MMset(m, 0, NIL);
130 return 0;
131 }
132
133 std::string pathStd = MMstartstr(m, MTOP(p));
134 if (pathStd.find(".zip") == std::string::npos)
135 {
136 MMechostr(MSKRUNTIME, "SO3AddResourceLocationZIP the file %s is not a .zip file", pathStd.c_str());
137 MMset(m, 0, NIL);
138 return 0;
139 }
140
141 std::string groupResource;
142 if (group == NIL)
143 groupResource = Ogre::RGN_DEFAULT;
144 else
145 groupResource = std::string(MMstartstr(m, MTOP(group)));
146
147 if (!checkGroupExists(groupResource))
148 {
149 MMechostr(MSKRUNTIME, "SO3AddResourceLocationZIP the group %s does not exist", groupResource.c_str());
150 MMset(m, 0, NIL);
151 return 0;
152 }
153
154 try
155 {
156 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(pathStd, "Zip", groupResource, true);
157 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(groupResource);
158 }
159 catch (Ogre::Exception &e)
160 {
161 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
162 MMset(m, 0, NIL);
163 return 0;
164 }
165
166 MMset(m, 0, ITOM(1));
167 return 0;
168}
169
170
180//TODO insert Groupmap in scene, Use scene name as group index
181int SO3GroupCreate(mmachine m)
182{
183#ifdef SO3_DEBUG
184 MMechostr(MSKDEBUG, "SO3GroupCreate\n");
185#endif
186
187 int group = MMpull(m);
188 int s = MMget(m, 0);
189 if ((group == NIL) || (s == NIL))
190 {
191 MMset(m, 0, NIL);
192 return 0;
193 }
194
195 SScene* scene = MMgetPointer<SScene*>(m, MTOP(s));
196 if (scene == NULL)
197 {
198 MMset(m, 0, NIL);
199 return 0;
200 }
201
202 std::string groupResource = std::string(MMstartstr(m, MTOP(group)));
203 groupResource = scene->GetName() + groupResource;
204
205 if (checkGroupProtected(groupResource))
206 {
207 MMechostr(MSKDEBUG, "SO3Engine Exception : Group is Protected \n");
208 MMset(m, 0, NIL);
209 return 0;
210 }
211
212 if (checkGroupExists(groupResource))
213 {
214 MMset(m, 0, NIL);
215 return 0;
216 }
217
218 try
219 {
220 SRoot::getSingletonPtr()->AddManualRessourceGroup(groupResource);
221 MMset(m, 0, ITOM(1));
222 }
223 catch (Ogre::Exception &e)
224 {
225 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
226 MMset(m, 0, NIL);
227 }
228
229 //TODO remove
234 packdir mypack = (packdir)SCgetExtra("FirstPack");
235 string pathStd;
236 std::vector<string> partitions;
237
238 while (mypack)
239 {
240 pathStd = mypack->path;
241 for (unsigned int i = 0; i < pathStd.length(); i++)
242 {
243 if (pathStd.substr(i, 1) == "\\")
244 pathStd.replace(i, 1, "/");
245 }
246 partitions.push_back(pathStd);
247 mypack = mypack->next;
248 }
249
250 //for default resources for unified shaders
251#ifdef _WIN32
252 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("Plugins/Ogre_Redist/OgreCore.zip", "Zip", groupResource, true);
253 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("Plugins/Ogre_Redist/rtshaderlib.zip", "Zip", groupResource, true);
254#elif defined(ANDROID)
255 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("ogre_redist/ogrecore.zip", "APKZip", groupResource, true);
256 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("ogre_redist/rtshaderlib.zip", "APKZip", groupResource, true);
257#elif defined(SO3_USE_OPENGLES2)
258 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("ogre_redist/ogrecore.zip", "Zip", groupResource, true);
259 Ogre::ResourceGroupManager::getSingleton().addResourceLocation("ogre_redist/rtshaderlib.zip", "Zip", groupResource, true);
260#else //linux
261std::string baseDir = (char*)SCgetExtra("scolExecDir");
262 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(baseDir + "/ogre_redist/ogrecore.zip", "Zip", groupResource, true);
263 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(baseDir + "/ogre_redist/rtshaderlib.zip", "Zip", groupResource, true);
264#endif
265
266 std::vector<string>::iterator iPartition = partitions.begin();
267 while (iPartition != partitions.end())
268 {
269 Ogre::ResourceGroupManager::getSingleton().addResourceLocation((*iPartition), "ScolFileSystem", groupResource, false);
270
271 iPartition++;
272 }
273
274 return 0;
275}
276
277
286int SO3GroupList(mmachine m)
287{
288#ifdef SO3_DEBUG
289 MMechostr(MSKDEBUG, "SO3GroupList\n");
290#endif
291
292 int k = 0;
293
294 int s = MMpull(m);
295 if (s == NIL)
296 {
297 MMpush(m, NIL);
298 return 0;
299 }
300
301 SScene* scene = MMgetPointer<SScene*>(m, MTOP(s));
302 if (scene == NULL)
303 {
304 MMpush(m, NIL);
305 return 0;
306 }
307
308 Ogre::StringVector myStr = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
309 std::vector<std::string> sceneGroups;
310
311 for (unsigned int i = 0; i < myStr.size(); i++)
312 {
313 std::string loc = myStr[i];
314 if ((loc.compare(0, scene->GetName().length(), scene->GetName()) == 0) && (loc.compare(scene->GetName().length(), loc.length(), Ogre::RGN_DEFAULT) != 0))
315 {
316 loc = loc.substr(scene->GetName().length());
317 sceneGroups.push_back(loc);
318 if (k = Mpushstrbloc(m, (char*)loc.c_str()))
319 return k;
320 }
321 }
322
323 if (MMpush(m, NIL))
324 return MERRMEM;
325
326 for (unsigned int i = 0; i < sceneGroups.size(); i++)
327 {
328 if (MMpush(m, 2 * 2))
329 return MERRMEM;
330 if (k = MBdeftab(m))
331 return k;
332 }
333 return 0;
334}
335
336
346int SO3GroupDelete(mmachine m)
347{
348#ifdef SO3_DEBUG
349 MMechostr(MSKDEBUG, "SO3GroupDelete\n");
350#endif
351
352 int group = MMpull(m);
353 int s = MMget(m, 0);
354 if ((group == NIL) || (s == NIL))
355 {
356 MMset(m, 0, NIL);
357 return 0;
358 }
359
360 SScene* scene = MMgetPointer<SScene*>(m, MTOP(s));
361 if (scene == NULL)
362 {
363 MMset(m, 0, NIL);
364 return 0;
365 }
366
367 std::string groupResource(MMstartstr(m, MTOP(group)));
368 groupResource = scene->GetName() + groupResource;
369
370 if (checkGroupProtected(std::string(groupResource)))
371 {
372 MMechostr(MSKRUNTIME, "SO3GroupDelete : Group is Protected \n");
373 MMset(m, 0, NIL);
374 return 0;
375 }
376
377 if (!checkGroupExists(groupResource))
378 {
379 MMset(m, 0, NIL);
380 MMechostr(MSKRUNTIME, "SO3GroupDelete : Group %s do not exist\n", groupResource.c_str());
381 return 0;
382 }
383
384 // meshes
385 const SNodeMap nodesListCopy = scene->GetNodeList();
386 SNodeMap::const_iterator iNodeList = nodesListCopy.begin();
387 while (iNodeList != nodesListCopy.end())
388 {
389 if (iNodeList->second->GetNodeType() == SNode::ENTITY_TYPE_ID)
390 {
391 SEntity* entity = static_cast <SEntity*> (iNodeList->second);
392 if (entity->GetGroupName() == groupResource)
393 OBJdelTH(m, SO3OBJTYPE, SCOL_PTR(entity));
394 }
395
396 iNodeList++;
397 }
398
399 //const Ogre::RenderSystemCapabilities* caps = SRoot::getSingletonPtr()->GetOgreRenderSystem()->getCapabilities();
400 //if (caps && caps->hasCapability(Ogre::RSC_FIXED_FUNCTION) == false)
401 {
402 // cleanup shaders before material (crash)
403 try
404 {
405 Ogre::RTShader::ShaderGenerator::getSingletonPtr()->removeAllShaderBasedTechniques();
406 }
407 catch (Ogre::Exception &e)
408 {
409 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
410 }
411 }
412
413 //$BB NEED to remove Scol and scene Resources from group ! material / textures / particle scripts ...
414 const SMaterialMap* matmap = scene->GetMaterials(groupResource);
415 if (matmap != 0)
416 {
417 SMaterialMap matmapCopy = (*matmap);
418 SMaterialMap::iterator iMaterialList = matmapCopy.begin();
419 while (iMaterialList != matmapCopy.end())
420 {
421 MMechostr(MSKDEBUG, ":>>> Delete SMaterial : %s\n", iMaterialList->second->GetName().c_str());
422 OBJdelTH(m, SO3MATERIAL, SCOL_PTR(iMaterialList->second));
423 iMaterialList++;
424 }
425 }
426 scene->CleanGroupMaterials(groupResource);
427
428 const STextureMap* texmap = scene->GetTextures(groupResource);
429 if (texmap != 0)
430 {
431 STextureMap texmapCopy = (*texmap);
432 STextureMap::iterator iTextureList = texmapCopy.begin();
433 while (iTextureList != texmapCopy.end())
434 {
435 OBJdelTH(m, SO3TEXTURE, SCOL_PTR(iTextureList->second));
436 iTextureList++;
437 }
438 }
439 scene->CleanGroupTextures(groupResource);
440
441 try
442 {
443 Ogre::ParticleSystemManager::getSingleton().removeTemplatesByResourceGroup(groupResource);
444 }
445 catch (Ogre::Exception &e)
446 {
447 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
448 }
449
450 try
451 {
452 SRoot::getSingletonPtr()->RemoveManualRessourceGroup(groupResource);
453 MMset(m, 0, ITOM(1));
454 }
455 catch (Ogre::Exception &e)
456 {
457 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
458 MMset(m, 0, NIL);
459 return 0;
460 }
461
462 return 0;
463}
464
474int SO3GroupReload(mmachine m)
475{
476#ifdef SO3_DEBUG
477 MMechostr(MSKDEBUG, "SO3GroupReload\n");
478#endif
479
480 int group = MMpull(m);
481 int s = MMget(m, 0);
482 if ((group == NIL) || (s == NIL))
483 {
484 MMset(m, 0, NIL);
485 return 0;
486 }
487
488 SScene* scene = MMgetPointer<SScene*>(m, MTOP(s));
489 if (scene == NULL)
490 {
491 MMset(m, 0, NIL);
492 return 0;
493 }
494
495 std::string groupResource(MMstartstr(m, MTOP(group)));
496 groupResource = scene->GetName() + groupResource;
497
498 if (checkGroupProtected(std::string(groupResource)))
499 {
500 MMechostr(MSKDEBUG, "SO3Engine Exception : Group is Protected \n");
501 MMset(m, 0, NIL);
502 return 0;
503 }
504
505 if (!checkGroupExists(groupResource))
506 {
507 MMset(m, 0, NIL);
508 return 0;
509 }
510
511 try
512 {
513 Ogre::ResourceGroupManager::getSingleton().unloadResourceGroup(groupResource);
514 Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup(groupResource);
515 Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(groupResource);
516 Ogre::GpuProgramManager::getSingletonPtr()->reloadAll(true);
517 Ogre::GpuProgramManager::getSingletonPtr()->reloadAll(true);
518 scene->O3TextureManager->reloadAll(true);
519 scene->O3MaterialManager->reloadAll(true);
520 scene->O3MeshManager->reloadAll(true);
521 scene->ReloadEntities();
522 MMset(m, 0, ITOM(1));
523 }
524 catch (Ogre::Exception &e)
525 {
526 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
527 MMset(m, 0, NIL);
528 return 0;
529 }
530
531 return 0;
532}
533
544//TODO REMOVE
546{
547#ifdef SO3_DEBUG
548 MMechostr(MSKDEBUG, "SO3AddResourceLocationDirectory\n");
549#endif
550
551 int p = MMpull(m);
552 int group = MMget(m, 0);
553 if (p == NIL)
554 {
555 MMset(m, 0, NIL);
556 return 0;
557 }
558
559 try
560 {
561 const char* groupResource;
562 if (group == NIL)
563 groupResource = Ogre::RGN_DEFAULT;
564 else
565 groupResource = MMstartstr(m, MTOP(group));
566
567 const char* resourceLocation = MMstartstr(m, MTOP(p));
568 /* SCOL Partition Manipulation */
569 /* NB : This function does not work when a check pack scol is calling so material can't be loaded with SO3SceneLoadResource */
570
571 packdir mypack = (packdir)SCgetExtra("FirstPack");
572 string pathStd;
573 std::vector<string> partitions;
574 while (mypack)
575 {
576 pathStd = mypack->path;
577
578 for (unsigned int i = 0; i < pathStd.length(); i++)
579 {
580 if (pathStd.substr(i, 1) == "\\") pathStd.replace(i, 1, "/");
581 }
582
583 partitions.push_back(pathStd);
584 mypack = mypack->next;
585 }
586
587 /* Adding resource location for all partitions with recursive boolean to "true" */
588 std::vector<string>::reverse_iterator iPartition = partitions.rbegin();
589 while (iPartition != partitions.rend())
590 {
591 (*iPartition) += resourceLocation;
592 Ogre::ResourceGroupManager::getSingleton().addResourceLocation((*iPartition), "ScolFileSystem", groupResource, true);
593
594 iPartition++;
595 }
596 MMset(m, 0, ITOM(1));
597 return 0;
598 }
599 catch (Ogre::Exception &e)
600 {
601 MMechostr(MSKDEBUG, "An exception has occurred: %s\n", e.what());
602 MMset(m, 0, NIL);
603 return 0;
604 }
605
606 return 0;
607}
608
609
610NativeDefinition natSO3Res[] = {
611 { "SO3GroupCreate", 2, "fun [SO3_SCENE S] I", SO3GroupCreate },
612 { "SO3GroupDelete", 2, "fun [SO3_SCENE S] I", SO3GroupDelete },
613 { "SO3GroupReload", 2, "fun [SO3_SCENE S] I", SO3GroupReload },
614 { "SO3GroupList", 1, "fun [SO3_SCENE] [S r1]", SO3GroupList },
615 { "SO3AddResourceLocationURL", 2, "fun [S S] I", SO3AddResourceLocationURL },
616 { "SO3AddResourceLocationZIP", 2, "fun [S P] I", SO3AddResourceLocationZIP },
617 { "SO3AddResourceLocationDirectory", 2, "fun [S S] I", SO3AddResourceLocationDirectory }
618};
619
620
626int SCOLloadResources(mmachine m, cbmachine w)
627{
628 return PKhardpak2(m, "SO3Res.pkg", sizeof(natSO3Res) / sizeof(natSO3Res[0]), natSO3Res);
629}
630
631
637{
638 return 0;
639}
640
int SCOLloadResources(mmachine m, cbmachine w)
Load the SO3Engine Resources function.
int SCOLfreeResources()
free the SO3Engine Viewport function
NativeDefinition natSO3Res[]
MMechostr(MSKDEBUG, " > Start loading Plugin SO3Engine dll\n")
SCOL_EXPORT int cbmachine w
Definition SO3SCOL.cpp:5150
int SO3MATERIAL
Definition SO3SCOL.cpp:97
bool checkGroupExists(std::string group)
function to check if an Ogre resource group exists
Definition SO3SCOL.cpp:955
int SO3OBJTYPE
Definition SO3SCOL.cpp:90
bool checkGroupProtected(std::string groupeName)
function to check if an Ogre resource group is protected
Definition SO3SCOL.cpp:974
int SO3TEXTURE
Definition SO3SCOL.cpp:98
int SO3GroupCreate(mmachine m)
SO3GroupCreate : Create a new resource group.
int SO3GroupReload(mmachine m)
SO3GroupReload : Reload a resource group.
int SO3AddResourceLocationDirectory(mmachine m)
SO3AddResourceLocationDirectory :This function add resource location directory in a group Deprecated
int SO3AddResourceLocationURL(mmachine m)
Main include.
int SO3GroupList(mmachine m)
SO3GroupList : Return a list of declared group.
int SO3AddResourceLocationZIP(mmachine m)
SO3AddResourceLocationZIP : Add resource zip in Ogre resource manager Deprecated
int SO3GroupDelete(mmachine m)
SO3GroupDelete : Delete a resource group.