/*
 * PVVMUD a 3D MUD
 * Copyright (C) 1998-1999  Programvareverkstedet (pvv@pvv.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <sys/param.h>
#include "pvvmud.H"
#include "clientcache.H"
#include "clisrvmanager.H"
#include "msggos.H"

CCliGeometryCache::CCliGeometryCache(CCliSrvManager * manager){
  m_manager = manager;
}

CObject * CCliGeometryCache::failFunc(int id){
  char fileName[MAXPATHLEN];

#ifdef USE_LOCAL_FILE_CACHE
  // Load from local cache if file exist
  sprintf(fileName,"goscache/%i.bog",id);
  FILE * file = fopen(fileName,"r");
  if (file != NULL){
    CGeometry * geo = m_manager->newGeometry(id); 
    geo->readBOG(file);
    fclose(file);
    return geo;
  } else {
    m_manager->request(GOSREQUEST_GEOMETRY,id);
    CCache::add(id,NULL);
  }
#else
  m_manager->request(GOSREQUEST_GEOMETRY,id);
  CCache::add(id,NULL);
#endif // USE_LOCAL_FILE_CACHE

  return NULL;
}

CCliMaterialCache::CCliMaterialCache(CCliSrvManager * manager){
  m_manager = manager;
}

CObject * CCliMaterialCache::failFunc(int id){
  char fileName[MAXPATHLEN];

#ifdef USE_LOCAL_FILE_CACHE
  // Load from local cache if file exist
  sprintf(fileName,"goscache/%i.bmat",id);
  FILE * file = fopen(fileName,"r");
  if (file != NULL){
    CMaterial * mat = m_manager->newMaterial(id);
    mat->load(fileName);
    fclose(file);
    return mat;
  } else {
    m_manager->request(GOSREQUEST_MATERIAL,id);
    CCache::add(id,NULL);
  }
#else
  m_manager->request(GOSREQUEST_MATERIAL,id);
  CCache::add(id,NULL);
#endif // USE_LOCAL_FILE_CACHE

  return NULL;
}

CCliTextureCache::CCliTextureCache(CCliSrvManager * manager){
  m_manager = manager;
}

CObject * CCliTextureCache::failFunc(int id){
  char fileName[MAXPATHLEN];

#ifdef USE_LOCAL_FILE_CACHE
  // Load from local cache if file exist
  sprintf(fileName,"goscache/%i.tex",id);
  FILE * file = fopen(fileName,"r");
  if (file != NULL){
    CTexture * tex = m_manager->newTexture(id);
    tex->load(fileName);
    fclose(file);
    return tex;
  } else {
    m_manager->request(GOSREQUEST_TEXTURE,id);
    CCache::add(id,NULL);
  }
#else
  m_manager->request(GOSREQUEST_TEXTURE,id);
  CCache::add(id,NULL);
#endif // USE_LOCAL_FILE_CACHE

  return NULL;
}